Collections in Java

Almost all programming languages have its own set of collections for making data storage more organized. Python has its own set of collections and C++ has its own STL library. Similarly, Java also has its collections framework to make its data storage part more ordered. Let’s take a deep dive into the world of collections framework in Java.

Collection Hierarchy

Talking about collection hierarchy in Java, there are mainly four key interfaces in the hierarchy and other classes or interfaces implement or extend them respectively. Those four interfaces are :

  • Collection
  • List
  • Queue
  • Set

Collection Interface

Collection interface is the interface at topmost level in the Collection Hierarchy. As it is a interface, we can’t create its object directly.

Some key methods of Collection interface are :

  • size() –  It returns an integer value denoting the number of elements present in the collection.
  • isEmpty() – It returns a boolean value stating whether the collection is empty or not.
  • contains(Object element) – It returns a boolean value stating whether the object passed as parameter to the method is present in the collection or not.
  • add(Object element) – It adds the object passed as parameter to the collection and returns true if the object was added. Otherwise, it returns false.
  • remove(Object element) – It removes the object passed as parameter from the collection and returns true if the object was removed. Otherwise, it returns false.
  • clear() – It removes all the collection elements and makes it empty.

List Interface

List interface comes just below the Collection interface in the collection hierarchy. List interface extends the Collection Interface.

Some methods in the list interface are :

  • add(int index,Object element) – It adds the object passed as parameter into the collection at the specified index.
  • addAll(int index,Collection c) – It adds the collection passed as parameter into the present collection at a particular index. It returns true if the collection was added. Otherwise, it returns false.
  • remove(int index) – It removes the object at specified index from the collection.
  • get(int index) – It returns the element present at the specified index in the collection.
  • set(int index,Object element) – The objective of this method is to replace an element from the collection. It replaces the object present in the specified index of the collection with the object passed as parameter.
  • indexOf(Object element) – It returns the index at which the object passed as parameter occurs first time in the collection (starting from 0th index). It returns -1 integer value if the element is absent from the collection.
  • lastIndexOf(Object element) – It returns the index at which the object passed as parameter occurs first time in the collection (starting from last index). It returns -1 integer value if the element is absent from the collection.

The classes that implement the list interface are :

  • ArrayList
  • LinkedList
  • Vector
  • Stack

Queue Interface

Queue interface is used to create a collection with FIFO(First In First Out) property i.e. the element that has been inserted first will be removed first from the collection. Some of the methods of Queue interface are :

  • add(Object element) – It adds the object that is passed as parameter to the collection and returns true if the object is added. But, it throws an exception if the element is not added.
  • offer(Object element) –It does the same as the add() method. The only difference is that it returns false if the element is not added.
  • remove() – It removes the element at the front of the collection and returns true if it is remove. It throws exception if the element is not successfully removed.
  • poll() – It does the same as the remove() method. The only difference is that it returns false if the element is not removed successfully.
  • element() – It returns the element that is in the front of the collection. It throws exception if there is no element in the collection.
  • peek() –  It does the same as the element() method. The only difference is that it returns false if there is no element in the collection.
  • PriorityQueue Class

PriorityQueue Class is used when a object is to be removed from a collection based on its priority. It uses a comparator to determine priority of each element. It extends AbstractQueue class.

PriorityQueue Example With Program

PriorityQueue Example
  • Deque Interface

Deque extends Queue interface. It has property of building a double ended queue. In a Deque, elements can be inserted or removed from both ends of the queue and hence, they can function both like stack or queue.

  • ArrayDeque Class

ArrayDeque demonstrates the application of the Deque Interface. The insertion and removal of elements in Array Deque can take place in two ends. The size of deque can also be changed. Insertion of null elements are prohibited in an ArrayDeque.

Set Interface

Set interface directly extends Collection Interface. Set interface does not let the insertion of duplicate elements. Set interface is extended by HashSet, LinkedHashSet and TreeSet classes. Some of the methods that are present in Set interface are :

  • add(Object element) – It adds the object passed as parameter if the object is not already present in the collection.
  • clear() – It deletes all the elements present in the collection and makes it empty.
  • contains(Object element) – It returns true if the object passed as parameter is present in the collection. Otherwise, it returns false.
  • size() – It returns an integer value denoting the number of elements present in the collection.
  • HashSet Class

HashSet class is used to create a collection that stores unique objects. Internally, it encourages the use of hash table for achieving this functionality. Hashset allows null value and is non-synchronized. Order of insertion of elements is not preserved in HashSet.

How HashSet Works?

Whenever we instantiate an object of HashSet in Java, it in turn instantiates an object of HashMap which stores all the object we insert into the HashSet as its key. The value of all the keys in the HashMap is set as a constant called “PRESENT” which is predefined in the HashSet implementation class. And this is the reason, HashSet only stores unique object because its internal implementation i.e. HashMap can’t have duplicate keys. This makes a clear concept in out mind that whenever an add() method is called on a HashSet, it internally calls a put() method for a HashMap.

HashSet Example With Program    

Collections Framework
HashSet Example

      

Leave a Reply