Introduction to Collection Framework

The Collection in Java is a framework that gives a structure to store and control a bunch of objects.

With the help of Java Collections, we can accomplish various operations on the data like –

  • Searching
  • Insertion
  • Sorting
  • Deletion
  • Manipulation

These all operations were available in Java previously too, but there were no set guidelines or framework that anyone could have followed. Initially, Java used to provide with Vector, Stack, Properties & Dictionary which used to help in performing the different kinds of Data manipulations.

Collections Framework came into existence with Java 1.2 which provided an architecture.

Following are the various advantages of Collection Framework:

  1. Code write is now much more Reusable
  2. Better Code Quality
  3. Efforts are reduced in designing a structure
  4. Developers can focus more on the Business logic now
  5. Helps in adapting or extending a collection easily
  6. Easy to understand for everyone

Due to the above benefits, Collection framework has gained a lot of popularity.

Hierarchy of Collection Framework

Below is the hierarchy of the Collection Framework. All the classes or the packages shown in the below illustration belongs to the java.util package.

Collection Hierarchy

Talking about collection hierarchy in Java, there are mainly four key interfaces in the hierarchy, namely Collection, List, Queue and Set.

Collection Interface

Collection interface is the interface at the topmost level in the Collection Hierarchy. As it is an interface, we cannot create its object directly. Instead, all other components in the hierarchy uses its methods. 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.

The classes that implement the list interface are: ArrayList, LinkedList, Vector, Stack. Let us discuss about some of these classes in detail:

  1. ArrayList Class

ArrayList is an alternative implementation of array with extended functionality. We do not have to specify the size of an ArrayList while initializing it. It can hold dissimilar items unless a particular type is specified. ArrayList class is found in java.util package.

  • Array vs ArrayList

A built-in java array cannot be modified. If we have to add more elements into an array, we need to create a new one. But in the case of an ArrayList, it is dynamic and hence, adapts the size on its own.

Advantages of ArrayList Over Arrays

  1. Adding or Deleting elements from a particular index in ArrayList is easier than doing the same in array.
  2. ArrayList class has multiple associated methods that can be used to perform various operations on the ArrayList.
  3. ArrayList can hold dissimilar objects if proper generic type is not specified.
  4. Using ListIterator, ArrayList can be traversed both forward and backwards.
  1. Iterator vs ListIterator

Iterator is an interface in Java which contains methods for traversing a collection in forward direction. It is present in java.util package. Some methods in the Iterator interface are:

  • hasNext() – It returns true if there are more elements present in the collection from the point where the cursor is currently pointing.
  • next() – It returns the next element that is present in the collection.
  • remove() – It removes the last element from the collection that was read by the iterator.

ListIterator is an interface in Java that extends the Iterator interface. It is also present in the java. util package. In addition to the methods present in the Iterator interface, ListIterator contains methods to traverse a collection in the backward direction and to add, remove, or modify elements at any position in the collection. Some methods in the ListIterator interface are:

  • add(Object element) – It adds the object passed as a parameter to the collection.
  • hasPrevious() – It returns true if the collection has more elements present while traversing in the backward direction. Otherwise, it returns false.
  • previous() – It returns the next object that is present in the collection while traversing in a backward direction.
  • Set(Object element) – It replaces the next element present in the collection with the element that is passed as parameter to the method.

Advantages of ListIterator over Iterator

  1. ListIterator helps in traversing a collection in the forward as well as in the backward direction.
  2. ListIterator allows us to add, remove, or replace an element at any position in the collection.
  1. Vector Class

Vector Class is an alternative of Java array that is dynamic in size. Vector class is synchronized i.e., if one thread is working on a vector then no other thread can use it at the same time. Vector has several methods implemented that are not present in the Collection or List interface.

ArrayList vs Vector

  1. Vector supports synchronization but ArrayList does not support it.
  2. Vector contains lots of legacy methods that are not a part of ArrayList.

Why not to use vector in your code?

We know that a Vector in Java is synchronized i.e., if a thread is using a Vector at a time then no other thread can take control on the same Vector at the same time. And hence, for implementation of this functionality, the use of Vector in the code is costlier than the use of ArrayList. But, if there is a need in our code for synchronization then Vector is the only option we have because ArrayList is not synchronized.

  1. LinkedList Class

The underlying data structure in case of a LinkedList Class in Java is a doubly linked list. It extends the AbstractList class and implements the Deque interface in addition to a List Interface. In a LinkedList, two elements are linked to each other through pointers.

As linked list implements both list and deque interfaces hence, LinkedList can also be used as a Queue.

ArrayList vs LinkedList

  1. An ArrayList is an implementation of an array internally whereas a LinkedList is an implementation of a doubly linked list.
  2. An ArrayList is comparatively slower than a LinkedList because manipulating data in an array is costlier than manipulating data in a doubly linkedlist.
  3. An ArrayList can be used in a case where accessing of data is needed frequently, whereas LinkedList can used in a case where data needs to be manipulated frequently.

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 the 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 works 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 removed. It throws an exception if the element is not successfully removed.
  • poll() – It works 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 an exception if there is no element in the collection.
  • peek() –  It works the same as the element() method. The only difference is that it returns false if there is no element in the collection.
  1. PriorityQueue Class

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

  1. Deque Interface

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

  1. ArrayDeque Class

ArrayDeque shows the application of the Deque interface. The instance of ArrayDeque can insert and remove elements from both ends. The size of the deque can also be changed. The insertion of null elements is prohibited in an ArrayDeque.

Set Interface

Set interface directly extends Collection Interface. Set interface does not permit the insertion of duplicate elements. HashSet, LinkedHashSet and TreeSet are some of the classes that extend Set interface. Some of the methods that are present in Set interface are:

  • add(Object element) – It adds the object passed as a 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.
  1. 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.

Tejasvi Nanda

About the Author

Tejasvi Nanda