Collection Interfaces in Java with Examples
In this tutorial, we will discuss collection interfaces in Java with Examples.
Introduction
The Collection Interface is an individual from the Java Collections Framework. It is a root point of interaction of the Collection Hierarchy. Any class doesn't straightforwardly execute the Collection Interface. Be that as it may, it is carried out roundaboutly through its subtypes or sub-interfaces like List, Queue, and Set. It is a piece of java.util package
For Example, the priority queue class executes the Queue interface, a piece of sub-interface of the Collection interface. On the off chance that an assortment execution doesn't carry out a specific activity, it ought to characterize the relating strategy to throw UnsupportedOperationException.
The following are the interfaces of the collection interfaces.
- List
- Set
- Queue
It carries out the Iterable<Ele> interface. The sub-connection points of Collection are BeanContext, BeanContextServices, BlockingDeque<Ele>, BlockingQueue<Ele>, Deque<Ele>, EventSet, List<Ele>, NavigableSet<Ele>, Queue<Ele>, Set<E>, SortedSet<Ele>, TransferQueue<Ele> .
Sub Interfaces of Collection Interface
Each Collection Framework Classes carries out the sub-Interfaces of the Collection Interface. Every strategy for the Collection interface is also contained in its sub-interfaces. These sub-Interfaces are, in some cases, called Collection Types or Subtypes of Collection.
List:
It is a sub-interface of the collection interface. This connection point or interface is given to the information of the list type in which we can store all the arranged collections of the objects. This additionally permits copy information to be available in it. This list interface is carried out by different classes like LinkedList, ArrayList, Stack, and vectors etc. We know that every subclass executes the list so let us instantiate an object.
Example:
List <Type> AL = new ArrayList<> ();
List <Type> LL = new LinkedList<> ();
List <Type> V = new Vector<> ();
Where Type refers to the type of the object.
Set:
A set is an unordered assortment of objects where redundant values can't be stored. This collection interface is utilized when we wish to avoid object redundancy and to hold just the extraordinary and unique objects. This set interface is implemented by different classes such as TreeSet, HashSet, etc. We know every subclass implements the set, and we can launch a set object with the following classes.
Syntax:
Set<Type> HS = new HashSet<> ();
Set<Type> LHS = new LinkedHashSet<> ();
Set<Type> TS = new TreeSet<> ();
Where Type refers to the type of the object.
SortedSet:
This interface is the same as the set interface, a sub-interface of the set interface. The main contrast is that this interface has additional techniques that keep up with the requests of the elements. The sorted set interface extends the set point of interaction and is utilized to deal with the information which should be sorted. The class which carries out this point of interaction is TreeSet. We know that Treeset can implement a sorted class. So let’s instantiate an object of the trees.
Example:
SortedSet<Type> TS = new TreeSet<> ();
Where Type refers to the type of the object.
Queue:
As the name recommends, a Queue interface follows the FIFO(First In First Out) principle like a genuine Queue. The queue interface is devoted to storing every element where the order of the elements matter. For instance, at whatever point we attempt to book a ticket, the tickets are sold at the early bird's worm premise. This way, the individual whose request shows up first in the line gets access. There are different classes like Deque, priority queue and ArrayDeque, etc. We have learned that the queue carries out all the various subclasses.
Example:
Line <Type> PQ = new PriorityQueue<> ();
Line <T> AD = new ArrayDeque<> ();
Where Type refers to the type of the object.
==
Deque:
This is a highly slight variety of the Queue structure. Deque is also called a double-ended queue, and it is a data structure in which we can add and eliminate the elements from both ends of the queue. This interface extends the Queue interface. ArrayDeque class is used to implement the deque. We know the Queue can execute the deque; we can launch a deque object with this class. Let’s instantiate an object of the queue to implement deque.
Example:
Deque<Type> AD = new ArrayDeque<> ();
Here, Type refers to the type of the object.
Program:
// program to describe Collection interface in java
// importing packages
import java.io.*;
import java.util.*;
public class JtpDemo {
public static void main(String s[])
{
// creating a Linked list by considering String as the type of object
Collection<String> lst = new LinkedList<String>();
// adding elements to the list
lst.add("Welcome");
lst.add("to");
lst.add("Tutorialandexample");
// Output the present list
System.out.println("The Created list is: " + lst);
// new elements in list
lst.add("tutorial");
lst.add("Kudos");
// Displaying new list
System.out.println("New List is: " + lst);
}
}
Output:
The list is: [Welcome, to, Tutorialandexample]
The new List is: [Welcome, to, Tutorialandexample, tutorial, Kudos]