Collections in Java – Types of Interface In Java
Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
In Java, a set or group of objects is known as a collection. Several interfaces, classes, and Algorithms together form the Collection Framework. Collections and the Collection framework are two very important concepts in Java when it comes to handling and organizing data. In this article, we will take a look at all the various collections that are present in the Java programming language and how we can implement them in our code.
Collections in Java
The collection is an interface that provides various other interfaces and classes that help store and manipulate groups of objects. It helps in various operations such as searching, sorting, insertion, manipulation, and deletion.
The Java Collection provides various interfaces, such as Set, List, Queue, Deque, and various classes, such as ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet, etc.
Java Collection Interface
The Collection interface is the root of the collection framework. Every other interface and class implements the Collection interface. It declares all the methods like Boolean add(Object obj), Boolean addAll (Collection c), void clear(), etc., which are implemented by all the subclasses of the Collection interface.
We will now discuss all the interfaces and classes that fall under the Collection Interface.
Java List Interface
List interface is a child interface of the Collection Interface. It stores data in the form of a list-type data structure. We can store an ordered collection of objects in it. It is possible to have duplicates in this type of data structure.
The classes ArrayList, LinkedList, Vector, and Stack implement the List Interface.
The List interface is instantiated using the following statements:
List <data-type> list1= new ArrayList();
List <data-type> list2 = new LinkedList();
List <data-type> list3 = new Vector();
List <data-type> list4 = new Stack();
Let us see the use of each of these classes individually.
Java ArrayList
The ArrayList class implements the List Interface to create a dynamic array. Thus, it is a more flexible form of the default static array. However, the ArrayList class always maintains the insertion order and is always non-synchronized. The ArrayList can be accessed randomly.
Code to Understand the Implementation of ArrayList:
package com.DataFlair.Collections;
import java.util.*;
public class Arraylist
{
public static void main(String args[])
{
ArrayList<Integer> arr=new ArrayList<Integer>();
arr.add(1);
arr.add(7);
arr.add(10);
arr.add(50);
Iterator i=arr.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}
The output of the above code:
7
10
50
Java LinkedList
The LinkedList also implements the List Interface. Internally, the LinkedList class uses a doubly linked list to store data. Therefore, it can also store duplicate elements and maintain the insertion order in a non-synchronized form. Although Data manipulation is faster in LinkedList because there is no shifting required.
Code to understand the implementation of LinkedList:
package com.DataFlair.Collections;
import java.util.*;
public class Linkedlist
{
public static void main(String args[])
{
LinkedList<Integer> L=new LinkedList<Integer>();
L.add(1);
L.add(7);
L.add(10);
L.add(50);
Iterator<Integer> i=L.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}
The output of the above code:
7
10
50
Java Vector
It is the same as the ArrayList in every aspect, except for the fact that vectors are synchronized and contain many methods that are not part of the Collection Framework.
Vectors in Java are a type of array; their size can increase or decrease. However, an array has a defined size at the time of creation. So, to overcome this constraint, a vector was introduced.
Code to understand the implementation of Vector:
package com.DataFlair.Collections;
import java.util.*;
public class Vectors
{
public static void main(String args[])
{
Vector<Integer> v=new Vector<Integer>();
v.add(1);
v.add(7);
v.add(10);
v.add(50);
Iterator<Integer> i=v.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
}
}
The output of the above code:
7
10
50
Java Stack
Stack is a subclass of the vector class. It follows the LIFO(Last In, First Out) principle of data structure. Thus, Stack contains all the methods of the Vector class and has methods like push(), peek(), and pop() of its own.
Code to understand the implementation of Stack:
package com.DataFlair.Collections;
import java.util.*;
public class Stacks
{
public static void main(String args[])
{
Stack<Integer> s = new Stack<Integer>();
s.push(1);
s.push(7);
s.push(10);
s.push(50);
s.push(100);
s.pop();
Iterator<Integer> itr=s.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
The output of the above code:
7
10
50
Java Queue Interface
The Queue Interface implements the Collection Interface. It maintains the FIFO(First In First Out) principle of Data Structure. It holds elements in an ordered list. Therefore, Various classes and interfaces implement the Queue Interface, like PriorityQueue, Deque, and ArrayDeque
The queue interface can be instantiated as follows:
Queue<String> q1 = new PriorityQueue();
Queue<String> q2 = new ArrayDeque();
Let us discuss the classes and interfaces that implement the Queue interface.
Java PriorityQueue
The PriorityQueue class implements the Queue interface. Thus, it holds the elements or objects by their priorities. Null values are not allowed in the PriorityQueue.
Code to implement PriorityQueue:
package com.DataFlair.Collections;
import java.util.*;
public class priority_queue
{
public static void main(String args[])
{
PriorityQueue<Integer> queue=new PriorityQueue<Integer>();
queue.add(1);
queue.add(7);
queue.add(10);
queue.add(50);
System.out.println("High Priority:"+queue.element());
System.out.println("High Priority:"+queue.peek());
System.out.println("Iterating through the queue :");
Iterator itr=queue.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
queue.remove();
queue.poll();
System.out.println("After removing two elements from the Queue:");
Iterator<Integer> itr2=queue.iterator();
while(itr2.hasNext()){
System.out.println(itr2.next());
}
}
}
The output of the above queue:
High Priority:1
Iterating through the queue :
1
7
10
50
After removing two elements from the Queue:
10
50
Java Deque Interface
The Deque Interface extends the Queue Interface. The Deque interface allows us to add and remove elements from both sides. However, it is basically a double-ended queue and is more flexible than a regular queue.
The deque interface can be instantiated by:
Deque d = new ArrayDeque();
Java ArrayDeque
The ArrayDeque class implements the Deque interface. It helps us implement the double-ended queue. However, ArrayDeque is faster than ArrayList and Stack and has no capacity restrictions, thus making it better and more flexible.
Code to understand the implementation of ArrayDeque:
package com.DataFlair.Collections;
import java.util.*;
public class ArrayDQ
{
public static void main(String[] args)
{
Deque<Integer> deque = new ArrayDeque<Integer>();
deque.add(1);
deque.add(7);
deque.add(10);
deque.add(50);
for (int i : deque)
{
System.out.println(i);
}
}
}
The output of the above code:
7
10
50
Java Set Interface
The Set interface also extends the Collection Interface. In a set, the list of elements is unordered, and the elements need to be unique, i.e, duplicates are not allowed. Only one Null value is allowed in a set. However, the set is implemented by the HashSet, LinkedHashSet, and TreeSet classes.
Set interface 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>();
Java HashSet
The HashSet class implements the Set Interface. It uses a hash table for storing elements. However, hashing is used to store the elements in a HashSet. However, each element in the HashSet is Unique. The insertion order is not maintained in HashSet.
Code to understand the implementation of HashSet:
package com.DataFlair.Collections;
import java.util.*;
public class Hashset
{
public static void main(String args[])
{
HashSet<Integer> set=new HashSet<Integer>();
set.add(1);
set.add(7);
set.add(10);
set.add(50);
Iterator<Integer> itr=set.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
The output of the above code:
50
7
10
Java LinkedHashSet
LinkedHashSet class implements the Set interface to represent the implementation of LinkedList. Thus, it extends the HashSet class. LinkedHashSet also has unique elements. Although it maintains the insertion order. Only one Null value is allowed.
Code to understand the implementation of LinkedHashSet:
package com.DataFlair.Collections;
import java.util.*;
public class LinkedHashset
{
public static void main(String args[])
{
LinkedHashSet<Integer> set=new LinkedHashSet<Integer>();
set.add(1);
set.add(7);
set.add(10);
set.add(50);
Iterator<Integer> itr=set.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
The output of the above code is:
7
10
50
Java SortedSet Interface
The SortedSet interface implements the Set interface. In this interface, the elements are stored in increasing order.
The SortedSet interface is instantiated by:
SortedSet<data-type> set = new TreeSet();
Java TreeSet
TreeSet is a class that extends the SortedSet Interface. Java TreeSet works by representing the data in a tree structure for data sorting and data storage. However, the tree set does not allow storing duplicate data. Hence, the element stored in the tree set must be unique, resulting in reduced data redundancy. Although elements are also stored in ascending order.
However, the tree set prohibits the use of null values; if added, it will throw an exception.
Code to understand the implementation of TreeSet:
package com.DataFlair.Collections;
import java.util.*;
public class Treeset
{
public static void main(String args[])
{
TreeSet<Integer> set=new TreeSet<Integer>();
set.add(1);
set.add(10);
set.add(50);
set.add(7);
Iterator<Integer> itr=set.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
The output of the above code is:
7
10
50
Summarizing the Java Collection Interface
| Interface | Collection Class | Ordered | Sorted |
| List | ArrayList | Indexed | No |
| Vector | Indexed | No | |
| LinkedList | Indexed | No | |
| Queue | Priority Queue | Sorted | By to-do order |
| Set | HashSet | No | No |
| TreeSet | Sorted | By natural order or custom order | |
| LinkedHashSet | By insertion Order | No |
Iterating Through Collections in Java
Java collections provide various ways to iterate through their elements. Here are two common approaches:
Iterators: The Iterator interface provides a way to iterate through a collection’s elements one at a time. Therefore, you can obtain an iterator from a collection using its iterator() method. The iterator has methods like hasNext() to check if there are more elements and next() to get the next element.
For-each loop: Java provides a more concise way to iterate through collections using the for-each loop. Thus, this loop automatically iterates over each element in the collection and assigns it to a temporary variable.
Conclusion
In this article, we saw the main interfaces that implement the Collection interface and their respective extended classes. Although Java Collections help store and maintain data with ease, knowing the concept of Java Collections makes the programs very easy to implement.
Did you like this article? If Yes, please give DataFlair 5 Stars on Google

