Collection Hierarchy in Java | Collection Interface

In this tutorial, we will know another important topic collection hierarchy in Java and collection interface.

In the previous tutorial, we have covered the basic points of the collections framework in Java with realtime examples, why do we need for collections framework if arrays are already available to store a group of objects.

If you have not familiar with all these concepts, I will recommend you to glance once.

Whenever you will go for any Java technical interview, an interviewer may ask you one question related to Java collection hierarchy diagram. So, let’s understand in depth.

Collection Hierarchy in Java

Collection Hierarchy in Java defines the relationships among various interfaces and classes that build up the Collections framework. It enables programmers to efficiently store, manipulate, and process data structures in Java.

The hierarchy of the entire collection framework consists of four core interfaces 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. The diagram of Java collection hierarchy is shown in the below figure.

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 in Java

1. 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).

It is placed at the top of the collection hierarchy in java. It provides the basic operations for adding and removing elements in the collection.

2. 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.

3. List, Queue, and Set have three components which extends the Collection interface. A map is not inherited by Collection interface.

List Interface

1. This interface represents a collection of elements whose elements are arranged sequentially ordered.

2. List maintains an order of elements means the order is retained in which we add elements, and the same sequence we will get while retrieving elements.

3. We can insert elements into the list at any location. The list allows storing duplicate elements in Java.

4. ArrayList, vector, and LinkedList are three concrete subclasses that implement the list interface.

Set Interface

1. This interface represents a collection of elements that contains unique elements. i.e, It is used to store the collection of unique elements.

2. 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.

3. Set does not allow any duplicate elements.

4. HashSet, LinkedHashSet, TreeSet classes implements the set interface and sorted interface extends a set interface.

5. It can be iterated by using Iterator but cannot be iterated using ListIterator.

SortedSet Interface

1. This interface extends a set whose iterator transverse its elements according to their natural ordering.

2. TreeSet implements the sorted interface.

Queue Interface

1. 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.

2. This interface represents a special type of list whose elements are removed only from the head.

3. LinkedList, Priority queue, ArrayQueue, Priority Blocking Queue, and Linked Blocking Queue are the concrete subclasses that implement the queue interface.

Deque Interface

1. A deque (double-ended queue) is a sub-interface of queue interface. It is usually pronounced “deck”.

2. This interface was added to the collection framework in Java SE 6.

3. Deque interface extends the queue interface and uses its method to implement deque. The hierarchy of the deque interface is shown in the below figure.Deque hierarchy in Java

4. 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.

5. LinkedList and ArrayDeque classes implement the Deque interface.

Map Interface

1. 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.

The hierarchy of the map interface is shown in the below figure.Map hierarchy in Java

2. Map uses a hashing technique for storing key-value pairs. It doesn’t allow to store the duplicate keys but duplicate values are allowed.

3. HashMap, HashTable, LinkedHashMap, TreeMap classes implements 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 in Java

The Collection interface consists of a total of fifteen methods for manipulating elements in the collection. They are as follows:

1. add(): This method is used to add or insert an element in the collection. The general syntax for add() method is as follow:

add(Object element) : boolean

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. The general syntax for this method is as follows:

addAll(Collection c) : boolean

3. clear(): This method clears or removes all the elements from the collection. The general form of this method is as follows:

clear() : void

This method returns nothing.

4. contains(): It checks that element is present or not in a collection. That is it is used to search an element. The general for contains() method is as follows:

contains(Object element) : boolean

This method 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. The general syntax is as follows:

containsAll(Collection c) : boolean

6. equals(): It checks for equality with another object. The general form is as follows:

equals(Object element) : boolean

7. hashCode(): It returns the hash code number for the collection. Its return type is an integer. The general form for this method is:

hashCode() : int

8. isEmpty(): It returns true if a collection is empty. That is, this method returns true if the collection contains no elements.

isEmpty() : boolean

9. iterator(): It returns an iterator. The general form is given below:

iterator() : Iterator

10. remove(): It removes a specified element from the collection. The general syntax is given below:

remove(Object element) : boolean

This method returns true if the element was removed. Otherwise, it returns false.

11. removeAll(): The removeAll() method removes all elements from the collection. It returns true if all elements are removed otherwise returns false.

removeAll(Collection c) : boolean

12. retainAll(): This method is used to remove all elements from the collection except the specified collection. It returns true if all the elements are removed otherwise returns false.

retainAll(Collection c) : boolean

13. size(): The size() method returns the total number of elements in the collection. Its return type is an integer. The general syntax is given below:

size() : int

14. toArray(): It returns the elements of a collection in the form of an array. The array elements are copies of the collection elements.

toArray() : Object[]

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.

toArray(Object array[]) : Object[]

Collections Class in Java

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 provide 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 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.

Key Points of Collection Hierarchy

Some key points of the collection hierarchy that you should keep in mind are as:

  • The key interfaces in collection hierarchy are List, Set, Queue, and Map.
  • The key implementation classes in the collection hierarchy are ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, and TreeMap.
  • The most commonly used methods of collection interface include add(), remove(), size(), isEmpty(), contains(), and clear().

this tutorial, you have learned about Collection Hierarchy in Java and Collection interface. We have covered almost all the key points related to collection hierarchy with diagram. Hope that you will have understood and enjoyed this tutorial.

In the next tutorial, we will understand the list interface in Java with the help of various examples.
Thanks for reading!!!

Next ⇒ Java List Interface⇐ Prev Next ⇒

Please share your love