Categories: Java

Interfaces of Java Collection framework

Numerous Interfaces in the Collections framework define the fundamental characteristics of distinct collection classes.

Collection Interface:

The interface that all classes in the collection framework implement is called the Collection interface. It identifies the methods each collection will contain. To put it another way, the Collection interface creates the basis upon which the collection framework is built.

Some methods of Collection interface:

  • boolean add (Object obj)
  • boolean addAll (Collection c)
  • void clear()
  • boolean contains(Object obj)
  • boolean isEmpty()
  • int size()
  • etc.
List Interface:

The child interface of the Collection interface is the List interface. A list-type data structure that we could use to hold the ordered collection of things is prevented by this. There might be duplicate values.

The classes ArrayList, LinkedList, Vector, and Stack implement the List interface.

to create an instance of the List interface:

List <datatype> list1= new ArrayList();  
List <datatype> list2 = new LinkedList();  
List <datatype> list3 = new Vector();  
List <datatype> list4 = new Stack();  

List Interface Methods:

  • Object get(int index)
  • int indexOf(Object obj)
  • int lastIndexOf(Object obj)
  • List subList(int start, int end)
The Queue Interface:

The order of first-in, first-out is maintained through the queue interface. It can be characterized as an ordered list used to store pieces that are scheduled to undergo processing. Many classes implement the Queue interface, including Priority Queue, Deque, and ArrayDeque.

A queue interface can be instantiated as follows:

Queue<String> qr1 = new PriorityQueue();  
Queue<String> qr2 = new ArrayDeque();  

Queue Interface Methods:

  • Object poll()
  • Object remove()
  • Object peek()
  • Object element()
The Dequeue Interface:

The Queue interface is expanded by the Deque interface. In Deque, we can remove and add parts from either side. A double-ended queue known by the abbreviation DEQUE allows us to perform operations at both ends.

Deque can be instantiated as follows:

Deque d = new ArrayDeque();  
Set Interface:

Set Interface in Java is present in java.util package. It extends the Collection interface. It represents the unordered set of elements that don’t allow us to store duplicate items. We can store at most one null value in the Set. Set is implemented by HashSet, LinkedHashSet, and TreeSet.

Set 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>();  

Note: also read about the Collections in Java

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago