×

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]

Related Topics

Java Queue Interface

Queue interface is a subtype of Collection interface. All methods in the Collection interface are also available in the Queue interface. It provides operations of Collection and also some additional...

2 minutes read.

Java List Implementations

ArrayList and LinkedList are the two implementations of List that are for general-purpose. You'll probably utilise an array list the majority of the time because it's quick and provides constant-time...

3 minutes read.

Cyclic Barrier in Java

Programmers often find it challenging to run multiple threads simultaneously. Java introduces the idea of concurrency, which enables us to run many threads concurrently, making this work simpler. Concurrent programming...

6 minutes read.

Sorting a String in Java

Sorting a String in Java Sorting is a process of arranging available data objects in a meaningful format that is ascending or descending order. Sorting a string or array of String...

4 minutes read.

Ganesha’s Pattern in Java

This part teaches us how to use stars and other special characters to write Ganesha's Pattern in Java programming. Among the most challenging Java pattern applications to code. The Ganesha will be...

3 minutes read.

How to run Java Program?

How to run Java Program In this section, we will learn how to write, compile and run a Java program in Command Promptusing notepad. In order to run a Java program, we...

2 minutes read.

Java String Interview Questions

Java String Interview Questions String is the most common and important discussed topics in a Java interview. In Java interview interviewer generally asked three to four questions based on String concept....

16 minutes read.

Java Snippets

The term "snippet" refers to a section of code that addresses numerous issues with just a few lines of code. Decreases the number of lines of code and improves programmer...

3 minutes read.

Java Xmx

This section will explain what Xmx in Java is and how to establish a Java application's maximum heap size. When we execute a Java application, it occasionally displays an error message...

3 minutes read.

Salesman Problem in Java

The Traveling Salesman Problem determines the shortest path that visits each city approximately once and loops back to the starting location. Another Java problem that is most like the Traveling...

5 minutes read.

Java Protected Keyword

An access modifier is a keyword that Java protects. It can be used to constructors, methods, inner classes, and variables. Variables, methods, and constructors that have been marked protected in...

3 minutes read.

How to stop execution after a certain time in Java

We will learn how to stop a long-running execution after a set amount of time in this post. We will take a look at a few alternative approaches to this...

6 minutes read.

User Defined Exception in Java

An exception is an error (run time error) that happened while a program was being executed. The program stops abruptly whenever the Exception occurs, and the code after the line...

3 minutes read.

Get yesterdays date by no of days in Java

In this tutorial, we are going to learn how to get yesterday’s date by the no of days in Java. Using the Calendar class, one can get the current date....

1 minute read.

Singleton class in Java

What is Singleton class in Java? Singleton means it is one. That means we can create only one instance or object of a class. For example, let us have a class...

3 minutes read.

Access modifiers in Java

Access modifiers in Java with Example In Java, there are two types of access modifiers one is a non-access modifier, and other is access modifier. If we talk about access modifier, there...

3 minutes read.

How to download Eclipse for Java

Introduction: We write a java program, and when we want to run it, we need software to run it. Eclipse is a kind of software used to execute a JavaFX...

3 minutes read.

Java Externalization

What is Externalization in Java? Externalization is a concept that is advanced to serialization. Serialization is a concept where we can transfer an object from our JVM ( Java virtual machine...

3 minutes read.

How to avoid deadlock in java

Deadlock: A deadlock is an event that never going to occur. In java, deadlock is just a part of the multithreading. It is an environment that allows us to run multiple...

4 minutes read.

Java Constant

A constant is an unchangeable entity in coding, as its title implies. The value which cannot be altered, in other terms. We shall understand about Java constants and exactly how...

3 minutes read.