10X Sale
kh logo
All Courses
  1. Tutorials
  2. Java

Java Collection

Updated on Sep 2, 2025
 
32,977 Views

The Java Collection is an architectural framework for storing and manipulating the group of objects.

  1. Java Collections can conduct all the activities you undertake on data such as search, sort, insert, manipulate, and delete.
  2. Java Collection refers to a single object unit. Java Collection Framework offers numerous interfaces (List, Set, Queue, Deque) and classes (TreeSet, Vector, LinkedList, PriorityQueue, ArrayList, HashSet).

Advantages of Collection Framework in Java:

  1. Increases program speed and quality: Increases performance by offering helpful data structures and algorithms with high-performance applications.
  2. Consistent API: The API has a number of fundamental interfaces such as Collection, Set, List, or Map. All classes that execute these interfaces (ArrayList, LinkedList, Vector, etc.) have some prevalent set of methods.
  3. Reduces programming effort: A java programmer doesn’t have to worry about the design of Collection framework, and he can concentrate on its best use in his program.

The java.util package includes all the Collection Framework classes and interfaces.

  1. Collection: It is a root interface having basic methods for example add(), remove(), contains(), isEmpty(), addAll() etc.
  2. List: It can contain duplicate elements and elements in the list that are ordered. Example: LinkedList and ArrayList.
  3. Set: Doesn't allow duplicate elements. Examples of Set interface implementations are HashSet (based on hashing) and TreeSet (based on balanced BST). Note that SortedSet is being implemented by TreeSet.
  4. Queue: Order elements typically in FIFO order except for exceptions such as PriorityQueue.
  5. Deque: It is possible to insert and remove elements at both ends. Allows LIFO as well as FIFO.
  6. Map: Contains pairs of key value. Does not permit duplicates. HashMap and TreeMap are examples of its implementation. SortedMap is implemented by TreeMap.

The distinction between the Set and Map interface is that we only have keys in Set, whereas key, value pairs are available in Map.

Image

Iterator interface in Java

Iterator interface only offers the ability to iterate the collection elements forward.

Methods of Iterator Interface

The Iterator interface has only 3 methods.

Method

Description

public Object next()

It returns the current element and moves the cursor pointer to the next element in collection object.

public boolean hasNext()

It returns true if the iterator has next element else it is false.

public void remove()

It removes the iterator's last returned element.

Iterable Interface

The Iterable interface for all collection classes is the root interface. The interface of collection extends the Iterable interface and thus all subclasses of the interface of collection also implement the Iterable interface.

  • It includes one abstract method only. Iterator<T> iterator()
  • It returns the iterator over Type T elements.

List Interface in Java

List interface is the Collection interface child. It inhibits a data structure of the list type in which we can store ordered object collection. It can have values duplicated.

The ArrayList, LinkedList, Vector, and Stack classes are implementing the List interface.

To instantiate the List interface, we must use:

List <data-type> listType1=newArrayList<data-type>();   
List <data-type> listType2 =newLinkedList<data-type>();   
List <data-type> listType3 =newVector<data-type>();   
List <data-type> listType4 =newStack<data-type>();  

In the List interface, there are different methods that can be used to insert, delete and access the elements from the list.

Below are the classes that implement the interface of the List.

ArrayList

The List interface is implemented by the ArrayList class. It utilizes a dynamic array to store various kinds of information duplicate elements. The class ArrayList retains the order of insertion and is not synchronized. It is possible to access randomly the elements stored in the ArrayList class.

Take the instance below.

import java.util.*;   
classTestJavaArrayList{   
public static voidmain(String args[]){   
ArrayList<String> myList=newArrayList<String>(); 
myList.add("Ashish"); 
myList.add("Rahul");   
myList.add("Shivam");   
myList.add("Montu");   
Iterator itr= myList.iterator();   
while(itr.hasNext()){   
System.out.println(itr.next());   
}   
}   
}  

Output of above program:

Ashish 
Rahul 
Shivam 
Montu

LinkedList

The Collection interface is implemented by LinkedList. It internally utilizes a dual-linked list to store the elements. The duplicate elements can be stored. It keeps the order of insertion and is not synchronized. The manipulation is quick in LinkedList because no shifting is necessary.

Take the instance below.

  import java.util.*;   
publicclass TestJavaLinkedList{   
public static voidmain(String args[]){   
LinkedList<String> linkedList =newLinkedList<String>();   
linkedList.add("Ashish");   
linkedList.add("Rahul");   
linkedList.add("Shivam");   
linkedList.add("Montu");   
Iterator<String> itr= linkedList.iterator();   
while(itr.hasNext()){   
System.out.println(itr.next());   
}   
}  
}   

Output:

Ashish 
Rahul 
Shivam 
Montu

Vector

To store the information elements, the Vector utilizes a dynamic array. It's ArrayList-like. It is synchronized, however, and includes many methods that are not part of the framework for collection.

Take the instance below.

 import java.util.*;   
publicclass TestJavaCollection3{   
public static voidmain(String args[]){   
Vector<String> myVector=newVector<String>();   
myVector.add("Sneha");   
myVector.add("Priya");   
myVector.add("Sonia");   
myVector.add("Preeti");   
Iterator<String> itr= myVector.iterator();   
while(itr.hasNext()){   
System.out.println(itr.next());   
}   
}   
}  

Output:

Sneha 
Priya 
Sonia 
Preeti

Stack

The stack is Vector's subclass. It uses the data structure of the last-in-first-out, i.e. Stack. The stack includes all Vector class methods and also offers its techniques such as boolean push), (boolean peek), (boolean push(object o) defining its characteristics.

Take the instance below.

  import java.util.*;   
publicclass TestJavaCollection4{   
public static voidmain(String args[]){   
Stack<String> myStack =newStack<String>();   
myStack.push("Ashish");   
myStack.push("Priya");   
myStack.push("Mohan");   
myStack.push("Sneha");   
myStack.push("Kangana");   
myStack.pop();   
Iterator<String> itr= myStack.iterator();   
while(itr.hasNext()){   
System.out.println(itr.next());   
}   
}   
}  

Output:

Ashish 
Priya 
Mohan 
Sneha

Queue Interface in Java

Queue interface keeps the first-in-first-out order. It can be described as an ordered list used to keep the processed elements. There are different classes that implement the Queue interface, such as PriorityQueue, Deque, and ArrayDeque.

Take the instance below.

Queue<Integer> queue1 =newPriorityQueue<Integer>();   
Queue<Integer> queue2 =newArrayDeque<Integer>();  

PriorityQueue

The Queue interface is implemented by the PriorityQueue class. It contains the components or items to be processed according to their priorities. PriorityQueue does not permit the storage of null values in the queue.

Take the instance below.

  import java.util.*;   
publicclass TestJavaQueue{   
public static voidmain(String args[]){   
PriorityQueue<String> myQueue=newPriorityQueue<String>();   
myQueue.add("Ashish ");   
myQueue.add("Vijay Raj");   
myQueue.add("JaiShankar");   
myQueue.add("Raj");   
System.out.println("Head element:"+ myQueue.element());   
System.out.println("Head element:"+ myQueue.peek());   
System.out.println("iterating the queue elements:");   
Iterator itr= myQueue.iterator();   
while(itr.hasNext()){   
System.out.println(itr.next());   
}   
myQueue.remove();   
myQueue.poll();   
System.out.println("after removing 2 elements:");   
Iterator<String> itr2=queue.iterator();   
while(itr2.hasNext()){   
System.out.println(itr2.next());   
}   
}   
}  

Output:

Head element:Ashish 
Head element:Ashish 
iterating the queue elements: 
Ashish 
Raj 
JaiShankar 
Vijay Raj 
after removing 2 elements: 
JaiShankar 
Vijay Raj

Deque Interface

Deque interface expands the interface to the queue. We can delete and add the components from both sides in Deque. Deque is a double-ended queue that allows us to execute both ends of the activities.

Take the instance below.

Deque deque  =newArrayDeque();

ArrayDeque

The Deque interface is implemented by the ArrayDeque class. It makes it easier for us to use the Deque. Unlike queue, from both ends we can add or delete the components. ArrayDeque is quicker than ArrayList and Stack and has no limitations on capability. Take the instance below.

  import java.util.*;   
publicclass TestJavaQueue{   
public static voidmain(String[] args){   
Deque<String> myDeque =newArrayDeque<String>();   
myDeque.add("Priya");   
myDeque.add("Sonia");   
myDeque.add("Rahul");   
for(String str : myDeque){   
System.out.println(str);   
}   
}   
}  

Output:

Priya 
Sonia 
Rahul

Set Interface in Java

The Java Set Interface package includes java.util. It expands the interface for the collection. It represents the unordered set of elements that do not allow the duplicate items to be stored. We can store a null value in Set as much as possible. HashSet, LinkedHashSet, and TreeSet implements the set.

Take the instance below.

Set<data-type> set1 =newHashSet<data-type>();   
Set<data-type> set2 =newLinkedHashSet<data-type>();   
Set<data-type> set3 =newTreeSet<data-type>();  

HashSet

Class HashSet implements Interface Set. It is the set that utilizes a storage hash table. Hashing is used to store HashSet components. It includes distinctive elements.

Take the instance below.

  import java.util.*;   
publicclass TestJavaSet{   
public static voidmain(String args[]){   
HashSet<String> mySet=newHashSet<String>();   
mySet.add("Ashish");   
mySet.add("Priya");   
mySet.add("Hemu");   
Iterator<String> itr= Myset.iterator();   
while(itr.hasNext()){   
System.out.println(itr.next());   
}   
}   
}  

Output:

Ashish 
Priya 
Hemu 

LinkedHashSet

LinkedHashSet class reflects Set Interface's LinkedList application. It expands the class of HashSet and implements the interface set. It also includes distinctive components such as HashSet. It keeps the order of insertion and allows null components.

Take the instance below.

  import java.util.*;   
publicclass TestJavaSet{   
public static voidmain(String args[]){   
LinkedHashSet<String> Myset=newLinkedHashSet<String>();   
Myset.add("Sonia");   
Myset.add("Kangana");   
Myset.add("Hemu");   
Iterator<String> itr= Myset.iterator();   
while(itr.hasNext()){   
System.out.println(itr.next());   
}   
}   
}  

Output:

Sonia 
Kangana 
Hemu

SortedSet Interface

SortedSet is the alternate of a Set interface that provides a total ordering on its elements. The elements of the SortedSet are arranged in the increasing (ascending) order. The SortedSet provides additional methods that inhibit the natural ordering of the elements.

Take the instance below.

SortedSet<data-type>set=newTreeSet data-type>();   

TreeSet

SortedSet is the Set interface alternative that offers a complete ordering of its components. The SortedSet components are arranged in the rising (ascending) order. The SortedSet offers extra methods that inhibit the elements ' natural order.

Take the instance below.

  import java.util.*;   
publicclass TestJavaSet{   
public static voidmain(String args[]){   
TreeSet<String> mySet=newTreeSet<String>();   
mySet.add("Raju");   
mySet.add("Vijay");   
mySet.add("Ashish");   
Iterator<String> itr= mySet.iterator();   
while(itr.hasNext()){   
System.out.println(itr.next());   
}   
}   
}  

Output:

Ashish 
Raju 
Vijay
+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Get your free handbook for CSM!!
Recommended Courses