Module-IV (Part-II):
Collection
by:
Dr. Soumya Priyadarsini Panda
Sr. Assistant Professor
Dept. of CSE
Silicon Institute of Technology, Bhubaneswar
Collections in Java
The Collection in Java is a framework that provides an architecture to
store and manipulate the group of objects.
Java Collections can achieve all the operations that you perform on a
data such as searching, sorting, insertion, manipulation, and deletion.
Java Collection framework provides many interfaces (Set, List, Queue,
Deque) and classes (ArrayList,Vector, LinkedList, PriorityQueue,
HashSet, LinkedHashSet, TreeSet).
What is Collection in Java?
A Collection represents a single unit of objects, i.e., a group.
What is a framework in Java
It provides readymade architecture.
It represents a set of classes and interfaces.
It is optional.
What is Collection framework
The Collection framework represents a unified architecture for storing
and manipulating a group of objects.
It has:
1. Interfaces and its implementations, i.e., classes
2. Algorithm
Hierarchy of Collection Framework
Import [Link] package for the Collection framework.
Topics to be covered
List Set
Queue
ArrayList HashSet
PriorityQueue
LinkedList
Stack
Methods of Collection interface
Cont…
Iterator interface
Iterator interface provides the facility of iterating the elements in a
forward direction only.
Iterable Interface
The Iterable interface is the root interface for all the collection
classes.
The Collection interface extends the Iterable interface and
therefore all the subclasses of Collection interface also implement
the Iterable interface.
It contains only one abstract method. i.e.,
Iterator<T> iterator()
It returns the iterator over the elements of type T.
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.
In other words, we can say that the Collection interface builds the
foundation on which the collection framework depends.
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.
List Interface
List interface is the child interface of Collection interface.
It inhibits a list type data structure in which we can store the
ordered collection of objects.
It can have duplicate values.
List interface is implemented by the classes
ArrayList, LinkedList, Vector, and Stack.
List Interface Cont...
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.
The classes that implement the List interface are given below
ArrayList
Java ArrayList class uses a dynamic array for storing the elements. It is
like an array, but there is no size limit.
Elements can be added or removed anytime. So, it is more flexible than
the traditional array.
The ArrayList class implements the List interface.
It uses a dynamic array to store the duplicate element of different data
types.
The ArrayList class maintains the insertion order and is non-
synchronized.
The elements stored in the ArrayList class can be randomly accessed.
import [Link].*;
Example
class TestJavaCollection1{
public static void main(String args[]){
ArrayList<String> list=new ArrayList<String>(); //Creating arraylist
[Link]("Ravi");//Adding object in arraylist
[Link]("Vijay");
[Link]("Ravi");
Output:
[Link]("Ajay");
//Traversing list through Iterator
Iterator itr=[Link]();
while([Link]()){
[Link]([Link]());
}
}
//For integers
Example
import [Link].*;
class TestJavaCollection1{
public static void main(String args[]){
ArrayList<Integer> list=new ArrayList<Integer>(); //Creating arraylist
[Link](10);
[Link](20);
[Link](30);
Output:
[Link](20);
//Traversing list through Iterator
Iterator itr=[Link]();
while([Link]()){
[Link]([Link]());
}
}
}
//For integers
Example
import [Link].*;
class TestJavaCollection1{
public static void main(String args[]){
ArrayList<Integer> list=new ArrayList<Integer>(); //Creating arraylist
[Link](10);
[Link](20);
[Link](30);
Output:
[Link](20);
//Printing the arraylist object
[Link](list);
}
}
}
Removing elements
For a list- list1 with elements: [10 20 30 40]
[Link](1);
//will remove the element present at index 1. i.e. 20
import [Link].*;
Example
class TestJavaCollection1{
public static void main(String args[]){
ArrayList<Integer> list=new ArrayList<Integer>(); //Creating arraylist
[Link](10);
[Link](20);
[Link](30);
[Link](20);
Output:
[Link](list); [10,20,30,20]
[Link](1); [10,30,20]
[10,30]
[Link](list);
[Link](2);
[Link](list);
}
}
LinkedList
LinkedList implements the Collection interface.
It uses a doubly linked list internally to store the elements.
It can store the duplicate elements.
It maintains the insertion order and is not synchronized.
In LinkedList, the manipulation is fast because no shifting is
required.
Example
import [Link].*;
public class TestJavaCollection2{
public static void main(String args[]){
LinkedList<String> al=new LinkedList<String>();
[Link]("Ravi"); Output:
[Link]("Vijay");
[Link]("Ravi");
[Link]("Ajay");
Iterator<String> itr=[Link]();
while([Link]()){
[Link]([Link]());
}
}
}
Logic for Merging 2 linked lists
LinkedList<Integer> list1=new LinkedList<Integer>();
LinkedList<Integer> list2=new LinkedList<Integer>();
LinkedList<Integer> list3=new LinkedList<Integer>();
[Link](10);
[Link](20);
[Link](30);
[Link](20);
[Link](list1);
[Link](list2);
[Link](list1);
[Link](list2);
[Link](list3);
Reversing a linked lists
LinkedList<Integer> list3=new LinkedList<Integer>();
[Link](10);
[Link](20);
[Link](30);
[Link](list3);
[Link](list3);
//this will reverse the elements of list3;
[Link](list3);
Output:
[10, 20, 30]
[30,20,10]
Stack
The stack is the subclass of Vector.
It implements the last-in-first-out data structure,
i.e., Stack.
The stack contains all of the methods of Vector class and also
provides its methods like-
boolean push(), boolean peek(), boolean push(object o), which
defines its properties.
Example
import [Link].*;
public class TestJavaCollection4{
public static void main(String args[]){
Stack<String> s = new Stack<String>();
[Link]("Ayush");
[Link]("Garvit");
Output:
[Link]("Amit");
[Link]("Ashish");
[Link]("Garima");
[Link]();
Iterator<String> itr=[Link]();
while([Link]()){
[Link]([Link]());
}
}}
Queue Interface
The Queue interface maintains the first-in-first-out order.
It can be defined as an ordered list that is used to hold the
elements which are about to be processed.
There are various classes like PriorityQueue, Deque, and
ArrayDeque which implements the Queue interface.
Queue interface can be instantiated as:
Queue<String> q1 = new PriorityQueue();
Queue<String> q2 = new ArrayDeque();
PriorityQueue
The PriorityQueue class implements the Queue interface.
It holds the elements or objects which are to be processed by their
priorities.
PriorityQueue doesn't allow null values to be stored in the queue.
Example
import [Link].*;
public class TestJavaCollection5{
public static void main(String args[]){
PriorityQueue<String> q=new PriorityQueue<String>();
[Link]("Amit Sharma");
[Link]("Vijay Raj");
[Link]("JaiShankar");
[Link]("Raj");
[Link]("head:"+[Link]());
[Link]("head:"+[Link]());
[Link]("iterating the queue elements:");
Iterator itr=[Link]();
while([Link]()){
[Link]([Link]());
}
Cont…
[Link]();
[Link]();
[Link]("after removing two elements:");
Iterator<String> itr2=[Link]();
while([Link]()){
[Link]([Link]());
}
}
}
HashSet
HashSet class implements Set Interface.
It represents the collection that uses a hash table for storage.
Hashing is used to store the elements in the HashSet.
It contains unique items.
Example
import [Link].*;
public class TestJavaCollection7{
public static void main(String args[]){
//Creating HashSet and adding elements
HashSet<String> set=new HashSet<String>();
[Link]("Ravi");
[Link]("Vijay");
[Link]("Ravi");
[Link]("Ajay"); Output:
//Traversing elements
Iterator<String> itr=[Link]();
while([Link]()){
[Link]([Link]());
}
}
}
LinkedHashSet
LinkedHashSet class represents the LinkedList implementation of Set
Interface.
It extends the HashSet class and implements Set interface.
Like HashSet, It also contains unique elements.
It maintains the insertion order and permits null elements.
Example
import [Link].*;
public class TestJavaCollection8{
public static void main(String args[]){
LinkedHashSet<String> set=new LinkedHashSet<String>();
[Link]("Ravi");
[Link]("Vijay");
[Link]("Ravi");
[Link]("Ajay"); Output:
Iterator<String> itr=[Link]();
while([Link]()){
[Link]([Link]());
}
}
}