Java Collection Hierarchy

The collection is a framework in java. It contains some of the most used classes which are present in the interfaces. Collection interface mainly has 3 child interfaces and these are as follows.

Java Collection Hierarchy

Image Source

1. List

It contains an order of all the objects. This interface also helps you to have a dynamic size of arrays according to the number of objects present. It is implemented by Vector, Stack, ArrayList, and LinkedList. Therefore we can create a list object with these classes.

ArrayList

Arraylist is a kind of dynamic array that stores values of all data types. It costs constant amortized time to add an element in ArrayList. Let’s look at the code to see different methods in ArrayList.

LinkedList

It uses a doubly linked list while storing the values, so getting the first & last value takes constant time. As compared to ArrayList the manipulation is faster because of storing data in the doubly linked list. Its methods are quite similar to the ArrayList.

Vector

Vector also works as a dynamic array. It is advised to use vector in thread-safe implementations otherwise we should use ArrayList in all other cases. Let’s look at the below code to see its functions.

Stack

Stack arranges the values by the last in the first out (LIFO) method. We can add elements by the push method and remove them by the pop method. The way of creating a stack is as follows.

2. Queue

It contains the objects in a specific order in which they are added to the queue. If the queue is empty initially and we add a value then it will be at the starting of the queue as long as the queue exists. This method is also known as First In First Out (FIFO). It is implemented by Deque, PriorityQueue, ArrayDeque.

3. Set

It is a collection of objects which doesn’t allow the same values in the same set i.e, each value will be different in a set. So whenever we need unique values we use them to set in our programs. It is implemented by TreeSet, LinkedHastSet, and many more. We can create set objects with these classes.

This was brief information about the collection hierarchy in Java. Comment down below if you have any queries.

Leave a Comment

Your email address will not be published. Required fields are marked *