×

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 Comparable Interface

We implement comparable interface when we need a new logic for determining equality. if two objects are equal, the equals() method returns true and compareTo() method returns 0. The basic...

1 minute read.

Sales Tax Problem in Java

To calculate the sales tax on a purchase in Java, you will need to know the following information: The cost of the item being purchased The sales tax rate You can then use...

4 minutes read.

Diffie Hellman Algorithm in Java

In this section, you will be acknowledged about Diffie Hellman algorithm clearly step wise along with an example and also an example program. Diffie Hellman Algorithm One of the most significant algorithms...

3 minutes read.

How to sort a String in Java

Sorting is the process of putting the elements in a certain order, either ascending or descending. Mostly the alphabetical order or natural order is used for a string. In other...

5 minutes read.

Java Integer toUnsignedString() method

The toUnsignedString() method of Java Integer class returns a string representation of the argument as an unsigned decimal value. The second syntax returns a string representation of the given argument as...

2 minutes read.

Various operations on HashSet in Java

In this article, you will be acknowledged about what is a HashSet in java and what are its operations in java programming language. The HashSet is a crucial part of...

3 minutes read.

Java callable example

What is callable in Java? Callable is an interface that is present in Java. There are two methods for constructing threads: one is to inherit the Thread class, while the other...

3 minutes read.

Java 8 Features

Java 8 Features: After the great success of Java 7, many developers were waiting for the next version of one of the best languages in the tech world. Moreover, on...

6 minutes read.

Set Matrix Zeros in Java

In coding round interviews, it is frequently asked as the most significant challenge. an m*n matrix is provided. Set the entire column and row of the matrix to 0 if any...

6 minutes read.

Difference between print() and println() in Java

In this tutorial, we will discuss the differences between print and println in Java language. There are mainly two methods to display the text on the console printprintln The above two methods are...

4 minutes read.

Sudoku in Java

Sudoku is a combinatorial-number-placement puzzle with a logic-based approach. The goal of a traditional Sudoku puzzle is to fill in the numbers on a 9 by 9 grid so that...

6 minutes read.

Camel Case in Java

Java names its classes, interfaces, methods, as well as variables using camel-case syntax. If the name consists of two words, the second word will always begin with a capital letter,...

3 minutes read.

Finding Odd Occurrence of a Number in Java

In this tutorial, we will learn how to find the odd occurrence of a number through a java program. Let us consider an array of non-negative integers. Here, except for one...

6 minutes read.

How to Import Packages in Java

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

Java Math cosh() Method

The cosh() method of Math class returns the first hyperbolic cosine((e+e)/2) of a double value. Syntax: public static double cosh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic cosine is to be...

2 minutes read.

Thread States in Java

An Item's Life Cycle (Thread States) A thread can be in any of the states mentioned above at any given time in Java. They are as follows: New Active I'm blocked or waiting Temporary...

3 minutes read.

Java Math multiplyFull() Method

The multiplyFull() method of Math class returns the exact product of the arguments. Syntax: public static long multiplyFull (int x, int y) Parameters: The parameters ‘x’ and ‘y’ represent the first value and second...

1 minute read.

Zebra Puzzle Problem in Java

Complex puzzles like the zebra puzzle demand a lot of work and mental training to complete. Because it was created by renowned German scientist Albert Einstein, it is also sometimes...

10 minutes read.

Java OCR

In this article, you will be acknowledged about what is a tesseract OCR, how it works, what are its used and advantages and disadvantages. Also, you will be able to...

4 minutes read.

Multiple Inheritance Programs in Java

A component of the object-oriented notion known as multiple inheritances allows a class to inherit properties from multiple parent classes. When methods that have the same signature are present in...

4 minutes read.