Scientech Easy Collection Hierarchy in Java | Collections class | Scientech Easy

Saturday, October 20, 2018

Collection Hierarchy in Java | Collections class

In this tutorial, we will learn a very important topic Collection Hierarchy in Java and Collections class. This topic is very important for an interview, especially for beginners.  

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 class hierarchy in java, Java collection framework hierarchy.
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 whose elements are arranged in sequentially ordered.
➲ We can insert elements into the list at any location. The list allows storing duplicate elements in Java.
➲ ArrayList, Vector, and LinkedList classes implement the list interface.

Set Interface
➲ This interface represents a collection that contains unique elements. 
➲ Set does not allow any duplicate elements. 
➲ All the elements in a set can be in any order. i.e Set doesn't maintain insertion order.
➲ HashSet, LinkedHashSet, TreeSet classes implements the set interface and sorted interface extends a set interface. 

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).
➲This interface represents a special type of list whose elements are removed only from the head. LinkedList and Priority queue classes implement the queue interface.

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 total 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 the 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:
We hope that this tutorial will help you to understand the Collection Hierarchy in Java and Collections class with methods explanation. Interviewer always asks questions related to this topic. So you prepare it to crack the interview, especially for beginners.

Next ➨ Java List Interface