Collection Hierarchy in Java | Collections class - Scientech Easy

Collection Hierarchy in Java | Collections class

In the previous tutorial, you have familiarized with the basic points of collection framework in Java. In this tutorial, we will learn an important topic collection hierarchy in java and collections class. Whenever you will go for any technical interview, an interviewer can ask you one question-related to collection hierarchy diagram. So, let's understand from basic.  

Collection Hierarchy in Java

The hierarchy of the entire collection framework consists of four core interface such as Collection, List, Set, Map, and two specialized interfaces named SortedSet and SortedMap for sorting. All the interfaces and classes for the collection framework are located in java.util package. 
Collection Hierarchy in Java
Collection Hierarchy in Java
e➝ extends, I➝ implements.
Extends: Extends is a keyword that is used for developing inheritance between two classes and two interfaces.
Implements: Implements is a keyword used for developing inheritance between class and interface.

Collection Interface
➲ The basic interface of the collections framework is the Collection interface which is the root interface of all collections in the API (Application programming interface) and placed at the top of the collection hierarchy. 
➲ Collection interface extends the Iterable interface. The iterable interface has only one method called iterator(). The function of the iterator method is to return the iterator object. Using this iterator object, we can iterate over the elements of the collection.  
➲ List, Queue, and Set have three component which extends the collection interface. A map is not inherited by the collection interface.

List Interface
➲ This interface represents a collection of elements whose elements are arranged in sequentially ordered.
➲ List maintains an ordered of elements means the order is retained in which we add elements, and the same sequence we will get while retrieving elements.
➲ We can insert elements into the list at any location. The list allows storing duplicate elements in Java.
➲ ArrayList, vector, and LinkedList are three concrete subclasses which implement list interface.

Set Interface
➲ This interface represents a collection of elements that contains unique elements. i.e, It is used to store the collection of unique elements.
➲ Set interface does not maintain any order while storing elements and while retrieving, we may not get the same order as we put elements.  All the elements in a set can be in any order.
➲ Set does not allow any duplicate elements. 
➲ HashSet, LinkedHashSet, TreeSet classes implements the set interface and sorted interface extends a set interface.
➲ It can be iterated by using Iterator but cannot be iterated using ListIterator. 

SortedSet Interface
➲ This interface extends a set whose iterator transverse its elements according to their natural ordering.
➲ TreeSet implements the sorted interface.

Queue Interface
➲ A queue is an ordered of the homogeneous group of elements in which new elements are added at one end(rear) and elements are removed from the other end(front). Just like a queue in a supermarket or any shop.
➲This interface represents a special type of list whose elements are removed only from the head. 
LinkedList, Priority queue, ArrayQueue, Priority Blocking Queue, and Linked Blocking Queue are the concrete subclasses which implement the queue interface.

Deque Interface
➲ A deque (double-ended queue) is a sub-interface of queue interface. It is usually pronounced "deck".
➲ This interface was added to the collection framework in Java SE 6.
➲ Deque interface extends the queue interface and uses its method to implement deque.
➲ It is a linear collection of elements in which elements can be inserted and removed from either end. i.e, it supports insertion and removal at both ends of an object of a class that implements it.
➲ LinkedList and ArrayDeque classes implement the Deque interface.Collection Hierarchy in Java
Map Interface
➲ Map interface is not inherited by the collection interface. It represents an object that stores and retrieves elements in the form of a Key/Value pairs and their location within the Map are determined by a Key.
➲ Map uses a hashing technique for storing key-value pairs. 
➲ It doesn't allow to store the duplicate keys but duplicate values are allowed.
➲ HashMap, HashTable, LinkedHashMap, TreeMap classes implements a Map interface.

SortedMap Interface
➲ This interface represents a Map whose elements are stored in their natural ordering. It extends the Map interface which in turn is implemented by TreeMap classes.

Methods of Collection Interface

The collection interface consists of a total of fifteen methods, They are as follows:
1. add(): This method is used to add or insert an element in the collection. If the element is added to the collection, it will return true otherwise false, if the element is already present and the collection doesn't allow duplicates. 

2. addAll(): This method adds a collection of elements to the collection. It returns true if the elements are added otherwise returns false.
3. clear(): Clears/remove all the elements from the collection.

4. contains(): It checks that element is present or not in a collection. That is it is used to search an element. It returns true if the element is present otherwise false.
5. containsAll(): This method checks that specified a collection of elements are present or not. It returns true if the calling collection contains all specified elements otherwise return false.

6. equals(): It checks for equality with another object. 
7. hashcode(): It returns the hash code number for the collection. Its return type is an integer.
8. isEmpty(): It returns true if a collection is empty.

9. iterator(): It returns an iterator.
10. remove(): It removes a specified element from the collection.
11. removeAll(): It removes all elements from the collection.

12. retainAll(): It is used to remove all elements from the collection except the specified collection.
13. size(): returns the total number of elements in the collection. Its return type is an integer.

14. toArray(): It returns the elements of a collection in the form of an array. The array elements are copies of the collection elements.
15. Object[ ] toArray(): Returns an array that contains all the elements stored in the invoking collection. The array elements are the copies of the collection elements.

Collections Class

The collections classes implement the collection interfaces. They are defined in java.util package. Some of the classes provide full implementations that can be used as it is. Others are abstract that provides basic implementations that can be used to create concrete collections. A brief overview of each concrete collection class is given below.

1. AbstractCollection: It implements most of the collection interface. It is a superclass for all of the concrete collection classes.
2. AbstractList: It extends AbstractCollection and implements most of the List interface.

3. AbstractQueue: It extends AbstractCollection and implements the queue interface.
4. AbstractSequentialList: It extends AbstractList and uses sequential order to access elements.

5. AbstractSet: Extends AbstractCollection and implements most of the set interface. 
6. ArrayList: It implements a dynamic array by extending AbstractList.

7. EnumSet: Extends AbstractSet for the use with enum elements.
8. HashSet: Extends AbstractSet for use with a hash table.

9. LinkedHashSet: Extends HashSet to allow insertion-order iterations.
10. LinkedList: Implements a linked list by extending AbstractSequentialList.

11. PriorityQueue: Extends AbstractQueue to support a priority based queue.
12. TreeSet: Extends AbstractSet and implements the SortedSet interface.

Final words
Hope that this tutorial has covered almost all the important points related to Collection Hierarchy in Java and Collections class with methods explanation. I hope that you will have enjoyed this tutorial.
Thanks for reading!
Next ➤ Java List Interface

⏪ Prev Next ⏩