0% found this document useful (0 votes)
4 views49 pages

Java Collection Framework Overview

Ok

Uploaded by

n65337516
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views49 pages

Java Collection Framework Overview

Ok

Uploaded by

n65337516
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Department of Applied Computational Science & Engg.

Program: [Link]-AIDS
Course Code: BCS-403
Course Name: Object Oriented Programming
with Java
Unit No. 4: JAVA COLLECTION FRAMEWORK
Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

Course Outcomes :
CO Number Title
CO1 Develop the object-oriented programming concepts using
Java
CO2 Implement exception handling, file handling, and multi-
threading in Java
CO3 Apply new java features to build java programs.
CO4 Analyze java programs with Collection Framework
CO5 Test web and RESTful Web Services with Spring Boot using
Spring Framework concepts

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

Course Prerequisites:

• Java interfaces, as many Collection classes implement


various interfaces
• Exception Handling, as many operations in the
Collection framework can throw exceptions

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

Syllabus

UNIT-4 ( JAVA COLLECTIONS FRAMEWORK)

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

What is a Framework?
• A framework is a set of classes and interfaces which provide a
ready-made architecture.
• a collection of classes such that all the classes perform the
same kind of task.

What is Collection in Java:


A Collection represents a single unit of objects, i.e., a group.

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

Collection, Collection Interface, Collections Class, Collection


Framework ?
Collection: A group of individual objects that represent a single entity is
known as a collection. It is the common word that you used in your daily
life.

Collection Framework: To represent a group of objects as a single


entity in the Java programming language we need classes and interfaces
defined by the Collection Framework.

Collection Interface: Interfaces specify what a class must do and not


how. It is the blueprint of the class. It is the root interface of the
Collection Framework that defines the most common methods that can
be used for any collection objects.

Collections Class: It is present in [Link] package and is a member


of Collection Framework. This class provides many utility methods for
the collection object.

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

Iterable Interface: It is the root interface for the entire collection framework.
The collection interface extends the iterable interface. The Iterable Interface allows
the collection to be iterated over. The collection interface extends the iterable
interface, hence the sub-classes of the collection interface also implement the
iterable interface, i.e., it automatically becomes a part of the iterable interface. It
contains only one abstract method i.e., Iterator<T> iterator()
It returns the iterator over the elements of type T.

Iterator Interface: Iterator is an object that can be used to loop through


collections.
As the name suggests, it is used to iterate over the elements. It is used to modify and
iterate over the elements in a collection.
There are 3 methods in the Iterator interface:
•public Object next()- It returns the next element in the collection. It throws the
exception of NoSuchElementException if there is no next element.
•public void remove()- It removes the current element from the collection.
•public boolean hasNext()- It returns true if there are more elements in the
collection. Else, returns false.

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

Collection Interface

The Collection interface is the interface which is implemented by all the classes in the
collection framework. It declares the methods that every collection will have. Some of
the methods of Collection interface are Boolean add( Object obj), Boolean
addAll(Collection c), void clear(), etc. which are implemented by all the subclasses of
Collection interface.

The following 6 interfaces are described below first later on been discussed with clean
java programs as in implementation.
•Collection interface
•List interface
•Queue interface
•Deque interface (Double-ended queue)
•Set interface
•Map

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

Methods of Collection interface

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

List Interface
i. List interface is the child interface of Collection interface.
ii. It inhibits a list type data structure in which we can store the ordered
collection of objects.
iii. It can have duplicate values.
iv. List interface is implemented by the classes ArrayList, LinkedList,
Vector, and Stack.
v. To instantiate the List interface, we must use :

List <data-type> list1= new ArrayList();


List <data-type> list2 = new LinkedList();
List <data-type> list3 = new Vector();
List <data-type> list4 = new Stack();

There are various methods in List interface that can be used to insert,
delete, and access the elements from the list.

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

ArrayList

i. The ArrayList class implements the List interface.


ii. It uses a dynamic array to store the duplicate element of different data types.
iii. It does not have a fixed size. Its size can be increased or decreased if elements
are added or removed.
iv. It can store all elements including Null.
v. It is not Thread-safe.
vi. The ArrayList class maintains the insertion order and is non-synchronized.
vii. The elements stored in the ArrayList class can be randomly accessed.
viii. Since the ArrayList cannot be used for primitive data types like int, char, etc. , we
need to use a wrapper class.
ix. Syntax for creating ArrayList:
ArrayList<String> list=new ArrayList<>();

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

Consider the following example for ArrayList:

[Link] [Link].*;
[Link] TestJavaCollection{
[Link] static void main(String args[]){
[Link]<String> list=new ArrayList<String>();//
Creating arraylist
[Link] is class which implements List interface (create and Array)
and Itrable Interface (traversal – working with index of the array)
[Link]("Ravi");//Adding object in arraylist list[0]=“Ravi”
[Link]("Vijay"); list[1] = “Vijay”
[Link]("Ravi"); list[2] = “Ravi”
[Link]("Ajay"); list[3] = “Ajay” OUTPUT: Ravi
10.//Traversing list through Iterator Vijay
[Link] itr=[Link](); Ravi
[Link]([Link]()){ Ajay
[Link]([Link]());
14.}
15.}
16.}
Program Name: [Link]-AIDS Program Code: 163
Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

import [Link].*;

public class AList{


public static void main(String args[]){

//creating an ArrayList
ArrayList<String> str= new ArrayList<String>();

//displaying the initial size


[Link]("Size at the beginning "+[Link]());

//add elements
[Link]("Hello");
[Link]("Hi");
[Link]("Namaste");
[Link]("Bonjour");

//displaying the ArrayList


[Link](str);

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

//displaying the size


[Link]("Size after addition "+[Link]());

//remove element at index 0


[Link](0);

//display the new ArrayList


[Link](str);

//display the new size


[Link]("Size after removal "+[Link]());
}
}
OUTPUT: Size at the beginning 0
[Hello, Hi, Namaste, Bonjour]
Size after addition 4
[Hi, Namaste, Bonjour]
Size after removal 3
Program Name: [Link]-AIDS Program Code: 163
Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

LinkedList
i. LinkedList implements the Collection interface.
ii. LinkedList uses Doubly Linked List to store its elements
while ArrayList internally uses a dynamic array to store its
elements.
iii. LinkedList is faster in the manipulation of data as it is
node-based which makes it unique.
iv. LinkedList is non-synchronized means multiple threads at a
time can access the code. This means if one thread is
working on LinkedList, other threads can also get a hold of it.
Multiple operations on LinkedList can be performed at a
time. For example, if addition is being performed by one
thread, other operation can be performed by some other
thread too.
v. It can store the duplicate elements.

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

Consider the following example for


import [Link].*;
LinkedList:

public class LList{


public static void main(String args[]){
//creating a LinkedList
LinkedList<String> list1= new LinkedList<String>();
//displaying the initial size
[Link]("Size at the beginning "+[Link]()); = 0
//add elements
[Link]("Java"); = java
[Link]("C++"); = java, C++
[Link]("JavaScript"); = Java, C++, JavaScript
[Link]("C#"); = C#, Java, C++, JavaScript
[Link]("Kotlin"); = C#, Java, C++, JavaScript, Kotlin
[Link](2,"Python"); = C#, Java, Python, C++, JavaScript, Kotlin
//displaying the LinkedList
[Link]("Original Linked List " + list1);

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

//displaying the size


[Link]("Size after addition "+[Link]());

//remove element at index 5


[Link](5);
[Link]("C#");

//display the new LinkedList


[Link]("New Linked List "+ list1); = Java, Python, C++, JavaScript

//display the new size


[Link]("Size after removal "+[Link]()); = 4
}
}

In the above code we used LinkedList methods like add(),


remove() and size() which perform manipulations on LinkedLists.

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

OUTPUT: Size at the beginning 0

Original Linked List [C#, Java, Python, C++, JavaScript, Kotlin]

Size after addition 6

New Linked List [Java, Python, C++, JavaScript]

Size after removal 4

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

Vector
i. Like ArrayList, Vectors in Java are used for dynamic arrays.

ii. It extends the AbstractList and implements the List interface.

iii. Vector is synchronised. Synchronised means only one thread at a time can
access the code. This means if one thread is working on Vector, no other
thread can get a hold of it. Only one operation on vector can be
performed at a time. For example, if addition is being performed by one
thread, other operation cannot be performed until the first one is over.

iv. Before the introduction of the Collection Framework, Vectors were


categorised as Legacy Classes(Classes which were a part of the earlier
release of Java but now they are re-constructed).

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

Consider the following example for Vector:

import [Link].*;
public class ExampleVector{
public static void main(String args[]){
//creating a Vector
Vector<Integer> v= new Vector<Integer>();

//displaying the size


[Link]("Size at the beginning "+[Link]()); = 0

//add elements
[Link](19);
[Link](88);
[Link](1);
[Link](39);
//displaying the Vector
[Link](v); 19, 88, 1, 39

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

//displaying the size


[Link]("Size after addition "+[Link]());

//remove element at index 3


[Link](3);

//display the new Vector


[Link](v);

//display the new size


[Link]("Size after removal "+[Link]());
}
}

OUTPUT: Size at the beginning 0


[19, 88, 1, 39]
Size after addition 4
[19, 88, 1]
Size after removal 3
Program Name: [Link]-AIDS Program Code: 163
Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

Stack
i. Stack class extends the Vector class and it is its subclass.
ii. It works on the principle of Last-In, First-Out.
iii. In order to put an object on the top of the stack, we call
the push() method.
iv. To remove and return the top element in the stack, we
call pop() method.
v. There are other methods like peek(), search() and
empty() which are used to perform operations on the stack.
vi. One thing to note is that Stack is thread-safe. It might be
overhead in an environment where the thread-safety concept is
not needed. So, ArrayDeque is preferred.

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

Consider the following example for Stack:

import [Link].*;
public class StackExample{
public static void main(String args[]){
//creating a Stack
Stack<Integer> s= new Stack<Integer>();

//displaying the initial size


[Link]("Size at the beginning "+[Link]()); = 0
//push elements
[Link](99);
[Link](28);
[Link](17);
[Link](74);
[Link](1);

//displaying the Stack


[Link]("New Stack" + s);

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

//displaying the size


[Link]("Size after addition "+[Link]());

//pop the element and display it


[Link]("Popped element " + [Link]());

//display the new Stack


[Link]("New Stack after popping"+ s);

//display the new size


[Link]("Size after removal "+[Link]());

//peek method to find the top-most element and display it


[Link]("Top-most element " + [Link]());

//the size remains the same as peek does not remove the element
[Link]("Size after Peek "+[Link]());
}
}

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

OUPUT: Size at the beginning 0


New Stack[99, 28, 17, 74, 1]
Size after addition 5
Popped element 1
New Stack after popping[99, 28, 17, 74]
Size after removal 4
Top-most element 74
Size after Peek 4

Explanation: In the above program saw methods like pop(),


push() and peek(). We are adding elements using pop() function,
removing the top using the push() function, and getting the top-
most element by the peek() function.

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

QUEUE
Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

Queue Interface
i. The Queue Interface extends the Collection interface.
ii. ii. It uses the principle of First-In, First-Out (FIFO).
iii. A Queue is an ordered list where there is a need to maintain the order of the
elements.
iv. It has classes like PriorityQueue and ArrayDeque.
v. The most famous implementation is that of PriorityQueue.

vi. Queue interface can be instantiated as:

1. Queue<String> q1 = new PriorityQueue();


2. Queue<String> q2 = new ArrayDeque();
Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

PriorityQueue
i. The PriorityQueue class extends AbstractQueue and implements the Queue
Interface.
ii. As the name suggests, they follow the principle of priority of the elements.
iii. We know that we follow First-In, First-Out for queues, but at times, the elements
need to be processed in terms of their priority. This is where the PriorityQueue
comes into play.
iv. It does not allow null values to be stored inside it.
v. The add() method is used to add an element while the poll() method is used to
remove the top-most element. While, peek() is used to display the top-most
element.

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

Consider the following example for PriorityQueue:

import [Link].*;
public class ScalerTopics{
public static void main(String args[])
{
// Creating a priority queue
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
//displaying the initial size
[Link]("Size at the beginning "+[Link]());
// Adding elements using add()
[Link](9);
[Link](29);
[Link](19);
[Link](7);
//displaying the PriorityQueue
[Link]("New PriorityQueue" + pq);

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

//displaying the size


[Link]("Size after addition "+[Link]());

// Printing the top element of the PriorityQueue


[Link]("Top-most element " +[Link]());

// Printing the top element and removing it


for(int i=[Link](); i>0; i--) {
[Link]("Removing " +[Link]());
[Link]("New PriorityQueue" + pq);
}
//displaying the PriorityQueue
}
}

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

OUTPUT: Size at the beginning 0


New PriorityQueue[9, 27, 99, 209]
Size after addition 4
Top-most element 9
Removing 9
New PriorityQueue[27, 209, 99]
Removing 27
New PriorityQueue[99, 209]
Removing 99
New PriorityQueue[209]
Removing 209
New PriorityQueue[]

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

Deque Interface
i. Deque interface extends the Queue interface.
ii. In Deque, we can remove and add the elements from both
the side.
iii. Deque stands for a double-ended queue which enables us
to perform the operations at both the ends.
iv. Deque can be instantiated as:
Deque d = new ArrayDeque();

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

ArrayDeque

• ArrayDeque class implements the Deque interface. It facilitates us to use the Deque.
• Unlike queue, we can add or delete the elements from both the ends.

[Link] [Link].*;
[Link] class TestJavaCollection6{
[Link] static void main(String[] args) {
4.//Creating Deque and adding elements
OUTPUT: Gautam
[Link]<String> deque = new ArrayDeque<String>();
Karan
Ajay
[Link]("Gautam");
[Link]("Karan");
[Link]("Ajay");
9.//Traversing elements
[Link] (String str : deque) {
[Link](str);
12.}
13.}
14.}

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

Set Interface
i. The Set interface defines an unordered collection.
ii. Set Interface in Java is present in [Link] package.
iii. It extends the Collection Interface.
iv. It cannot store duplicate values in this.
v. It can store at most one null value in Set.
vi. The Set Interface is implemented by popular classes like HashedSet,
LinkedHashSet, and TreeSet.
vii. Set can be instantiated as:
Set<data-type> s1 = new HashSet<data-type>();
Set<data-type> s2 = new LinkedHashSet<data-
type>();
Set<data-type> s3 = new TreeSet<data-type>();

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

Set Interface

[Link] [Link].*;
[Link] class setExample{
3. public static void main(String[] args)
4. {
5. // creating LinkedHashSet using the Set
6. Set<String> data = new LinkedHashSet<String>();
7.
8. [Link](“CSDS1");
9. [Link](“CSDS2");
10. [Link](“CSDS3");
11. [Link](“CSDS4");
12.
13. [Link](data);
14. }
15.}

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

Operations of Set Interface

On the Set, we can perform all the basic mathematical operations like
intersection, union and difference.
Suppose, we have two sets, i.e., set1 = [22, 45, 33, 66, 55, 34, 77] and set2 =
[33, 2, 83, 45, 3, 12, 55]. The following operation can be performed on the Set:

•Intersection: The intersection operation returns all those elements which are present in both the
set. The intersection of set1 and set2 will be [33, 45, 55].
•Union: The union operation returns all the elements of set1 and set2 in a single set, and that set
can either be set1 or set2. The union of set1 and set2 will be [2, 3, 12, 22, 33, 34, 45, 55, 66, 77,
83].
•Difference: The difference operation deletes the values from the set which are present in another
set. The difference of the set1 and set2 will be [66, 34, 22, 77].
•Set1 – set2 = set2 – set1
•[22, 66, 34, 77] != [2, 83, 3, 12]

•In set, addAll() method is used to perform the union, retainAll() method is used to perform the
intersection , removeAll() method is used to perform difference.

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

Operations of Set Interface


package [Link];
import [Link].*;
public class SetExample1 {
public static void main(String args[])
{
Integer[] A = {22, 45,33, 66, 55, 34, 77};
Integer[] B = {33, 2, 83, 45, 3, 12, 55};

Set<Integer> set1 = new HashSet<Integer>();


[Link]([Link](A));

Set<Integer> set2 = new HashSet<Integer>();


[Link]([Link](B));

// Finding Union of set1 and set2


Set<Integer> union1 = new HashSet<Integer>(set1);
[Link](set2);
[Link]("Union of set1 and set2 is:");
[Link](union1);

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

Operations of Set Interface


// Finding Intersection of set1 and set2
Set<Integer> intersection1 = new HashSet<Integer>(set1);
[Link](set2);
[Link]("Intersection of set1 and set2 is:");
[Link](intersection1);
// Finding Difference of set1 and set2
Set<Integer> difference1 = new HashSet<Integer>(set1);
[Link](set2);
[Link]("Difference of set1 and set2 is:");
[Link](difference1);
}
}

Output:
Union of set1 and set2 is:[33, 66, 34, 2, 83, 3, 22,
55, 12, 45, 77]
Intersection of set1 and set2 is:[33, 55, 45]
Difference of set1 and set2 is:[66, 34, 22, 77]

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

Method and Description

boolean add(E e)Adds the specified element to this set if it is not already present (optional
operation).
boolean addAll(Collection<? extends E> c)Adds all of the elements in the specified collection to
this set if they're not already present (optional operation).
void clear()Removes all of the elements from this set (optional operation).
boolean contains(Object o)Returns true if this set contains the specified element.
boolean containsAll(Collection<?> c)Returns true if this set contains all of the elements of the
specified collection.
boolean equals(Object o)Compares the specified object with this set for equality.
int hashCode()Returns the hash code value for this set.
boolean isEmpty()Returns true if this set contains no elements.
Iterator<E> iterator()Returns an iterator over the elements in this set.
boolean remove(Object o)Removes the specified element from this set if it is present
(optional operation).
boolean removeAll(Collection<?> c)Removes from this set all of its elements that are
contained in the specified collection (optional operation).
boolean retainAll(Collection<?> c)Retains only the elements in this set that are contained in
the specified collection (optional operation).
int size()Returns the number of elements in this set (its cardinality).
default Spliterator<E> spliterator()Creates a Spliterator over the elements in this set.
Object[] toArray()Returns an array containing all of the elements in this set.
<T> T[] toArray(T[] a)Returns an array containing all of the elements in this set; the
runtime type of the returned array is that of the specified array.

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

HashSet

•Java HashSet class implements the Set interface, backed by a hash table
which is actually a HashMap instance.
•The underlying data structure for HashSet is Hashtable.
•As it implements the Set Interface, duplicate values are not allowed.
•Objects that you insert in HashSet are not guaranteed to be inserted in the same order. Objects
are inserted based on their hash code.
•NULL elements are allowed in HashSet.
.

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

LinkedHashSet

• The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked


List across all elements.
• When the iteration order is needed to be maintained this class is used.
• When iterating through a HashSet the order is unpredictable, while a
LinkedHashSet lets us iterate through the elements in the order in which they were
inserted.
• When cycling through LinkedHashSet using an iterator, the elements will be
returned in the order in which they were inserted.

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

TreeSet

• TreeSet is one of the most important implementations of the SortedSet


interface in Java that uses a Tree for storage.
• The ordering of the elements is maintained by a set using their natural
ordering whether or not an explicit comparator is provided.
• Since a set doesn’t retain the insertion order, the navigable set interface
provides the implementation to navigate through the Set.
• The class which implements the navigable set is a TreeSet which is an
implementation of a self-balancing tree.
• Therefore, this interface provides us with a way to navigate through this
tree.

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

HashSet Interface

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : BCS-403 Course Name: OOP with Java

HashSet Interface

Program Name: [Link]-AIDS Program Code: 163


Department of Applied Computational Science & Engg.
Course Code : Course Name:

Recommended Books
Text books: Java The Complete Reference
“Java Black Book”

Program Name: Program Code:


THANK YOU

You might also like