A brief overview of Java Collection Hierarchy

Reading Time: 4 minutes

The collections framework was introduced in Java 2 or Java 1.2, from then onward it became a very popular part of the Java platform.

First, let’s start with an understanding of what is collection framework is. It is an object which represents a group of objects. It provides you with the ready-to-use most popular data structures like Linked list, Stack, Maps, etc, and their implementation.

Advantages of using Collection Framework

  1. It provides a ready-to-use data structure and its implementation.
  2. Collection Framework increases the performance due to the high-quality implementation of the data structure provided in the Collections framework.
  3. It makes your code easy to maintain.

Collections hierarchy

Set Interface

The set interface does not contain duplicate values. It models the mathematical set abstraction set interface that contains only methods which are inherited from the Collection interface.

Some methods:

add( ) – Adds an object in the collection.

clear( ) –Removes all objects in the collection.

contains( ) – Returns true if the given object is an element within the collection.

Output

List Interface

The List interface can contain duplicate elements and is implemented by classes like :

  1. ArrayList
  2. LinkedList
  3. Vector
  4. Stack

Some methods:

add( ) – Adds an object at the end of the collection. clear( ) – Removes all objects in the collection

size() – Returns the number of elements in this list.

output

Queue Interface

The queue interface follows FIFO (First in First out) order, the only exception is Priority Queue.

The queue implementation does not allow null elements, the only exception is LinkedList Queue.

Some methods:

element() – It fetches the element of the head of the queue but does not remove it from the queue.

poll() – It fetches the element of the head of the queue and removes it from the queue.

Output

Deque Interface

The Deque Interface extends the queue Interface.

The Deque(Double-ended queue)

It can use LIFO(Last in First Out) and FIFO(First In First Out).

Some methods:

add() – It will add an element at the last of the deque.

push() – It will add an element at the head of the deque.

Output

SortedMap Interface

It takes two parameters Key and Value pair

Null key or value is not allowed SortedMap Interface

The keys are Sorted in Natural order (order of the string in Alphabetical order) or specified comparator.

Some methods:

entrySet() – returns a Set view of the mapping contained in the map.

values() – returns a Collection view of the values contained in the map.

Output

SortedSet Interface

It stores the elements in the sorted set in sorted order(Natural ordering).

NavigableSet Interface extends SortedSet.

Some methods:

first() – It returns the First or lowest element of the Set.

last()- It returns the Last or highest element of the Set.

Output

Summary

In this blog, we covered about collection framework and the different types of data structures it provides for example array, linked list, set, etc.

Resources

https://docs.oracle.com/javase/8/docs/technotes/guides/collections/overview.html

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading