Last Updated on September 12, 2020 by Scientech Easy
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 can ask you one question-related to Java collection hierarchy diagram. So, let’s understand the basics.
Collection Hierarchy 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.
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
➲ 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.
➲ The 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.
➲ This interface represents a collection of elements whose elements are arranged sequentially ordered.
➲ 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.
➲ We can insert elements into the list at any location. The list allows storing duplicate elements in Java.
➲ 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.
➲ This interface extends a set whose iterator transverse its elements according to their natural ordering.
➲ TreeSet implements the sorted 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 that implements the queue 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.
➲ 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.
➲ 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 Map 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 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 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.
Hope that this tutorial has covered almost all the important points related to Collection Hierarchy in Java and Collection interface with methods explanation. I hope that you will have understood and enjoyed this tutorial.
Thanks for reading!!!