Collections in Java – Types of Interface In Java

Free Java courses with 37 real-time projects - Learn Java

In java, a set or group of objects is known as Collections. There are several Interfaces, Classes and Algorithms that together form the Collection Framework. Collections and Collection framework are two very important concepts in java when it comes to handling and organizing the 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 helps 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. Each and 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 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 implements 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. It is a more flexible form of the default static array. 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:

1
7
10
50

Java LinkedList:

The LinkedList also implements the List Interface. Internally, the LinkedList class uses a doubly linked list to store data. It can also store duplicate elements and maintains the insertion order in a non-synchronized form. 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:

1
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.

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:

1
7
10
50

Java Stack:

A subclass of the vector class is Stack. It follows the LIFO(Last in First Out) principle of data structure. Stack contains all the methods of the Vector class and has methods like push(), peek(), 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:

1
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. There are various classes and interfaces that implement the Queue Interface, like PriorityQueue, Dequeue and ArrayDeque

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. 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
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. 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. 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:

1
7
10
50

Java Set Interface:

The Set interface also extends the Collection Interface. In set, the list of elements are unordered, also the elements need to be unique, i.e, duplicates are not allowed. Only one Null value is allowed in a set. Set is implemented by the HashSet, LinkedHashSet, 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:

HashSet class implements the Set Interface. It uses a hash table for storing elements. Hashing is used to store the elements in HashSet. 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:

1
50
7
10

Java LinkedHashSet:

LinkedHashSet class implements the Set interface to represent the implementation of LinkedList. It extends the HashSet class. LinkedHashSet also has unique elements. 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:

1
7
10
50

Java SortedSet Interface:

The SortedSet interface implements the Set interface. In this interface, the elements are stored in increasing order(ascending).

The SortedSet interface is instantiated by:

SortedSet<data-type> set = new TreeSet();  

Java TreeSet:

The TreeSet class extends the SortedSet interface. It uses the tree data structure to store data. The elements in this are also unique, but the elements are stored in ascending order.

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:

1
7
10
50

Summarizing the Java Collection Interface:

InterfaceCollection ClassOrderedSorted
ListArrayListIndexedNo
VectorIndexedNo
LinkedListIndexedNo
QueuePriority QueueSortedBy to-do order
SetHashSetNoNo
TreeSetSortedBy natural order or custom order
LinkedHashSetBy insertion OrderNo

Quiz on Collections in Java

Conclusion:

In this article, we saw the main interfaces that implement the Collection interface and their respective extended classes. Java Collection helps store and maintain data at an ease, thus knowing the concept of Java Collections, makes the programs very easy to implement.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *