What are collection in Java

The collection provides a seamless architecture to store and manipulate the group of objects. Using the Collection framework, we can perform any operation on the data like sorting, searching, manipulation etc.

Collection means binding of objects into a single unit.

Framework means an architecture which is readymade, and which represents the set of classes and objects.

Collection Framework means a predefined architecture which helps to perform different data manipulation operations on the collection of objects.

 

Collection Framework Hierarchy

The base package is java.util. The collection is the base class for the whole Collection framework which extends Iterable class. Collection hierarchy is mainly divided into three interfaces:

        ·       List

        ·       Queue

        ·       Set


Now, we will discuss these interfaces and classes one by one. We will also see how they can be used, scenarios where we can use each one of them, some of their internal methods. Let’s begin.

        1.    Iterator Interface

This interface provides a convenient way to iterate over any collection. Using iterator we can only travel in one direction.

Internal methods of Iterator interface

·       public Object next() – returns the Object(element) and forward the cursor to next element.

·       public boolean hasNext() – checks for the presence of the next element.

·       public void remove() – used to remove the element.

 

        2.    Iterable Interface

Iterable Interface comes at the top of the collection hierarchy. It is the super interface. It has only one abstract method

   Iterator<T> itrerator();

 

        3.    Collection Interface

Collection Interface is the foundation interface for all the collection classes. The classes which implement the collection interface will have all its methods. Some of the methods that are present in the collection framework are

·       Boolean add(Object obj)

·       Boolean addAll( Collection c)


        4.    List Interface

List interface extends Collection interface. The list interface has a list data structure which can store collection of objects. It allows duplicate values to be stored.

ArrayList, LinkedList, Vector and Stack are the classes which implement List Interface.

We can instantiate the list interface in the following ways:

·       List<Object> list1 = new ArrayList();

·       List<Object> list2 = new LinkedList();

·       List<Object> list3 = new Stack();

·       List<Object> list4 = new Vector();

The classes which implement List Interface are as follows:

 

        5.    ArrayList

ArrayList implements the List interface. It is present in java.util package. ArrayList is a resizable data structure. The main difference between built-in array and ArrayList is that ArrayList supports dynamic sizing. Which means we can remove and add the elements into the ArrayList anytime.

Below program shows a way to use ArrayList

Output:

Ravi

Ram

Ravi

Rajeev

 

Internal methods of ArrayList

·       add(Object o) – use to add objects into the ArrayList

·       add(int index, Object o) – use to add an object to a specified ArrayList index.

·       int indexOf(Object o) – returns the index of the object specified.

·       remove(Object o) – use to remove the specified object.

 

        6.    LinkedList

LinkedList implements the List interface. Internally it uses doubly LinkedList to store the elements. Duplicates are allowed in this Collection also. The insertion order is maintained. Every LinkedList element is divided into two parted node and the address. The actual data is stored into the node and the pointer to the next element is stored in the address part.


The above diagram depicts the Singular Linked List. There are many types of LinkedList. For the explanation, I have posted this as a separate post “LinkedList in Java”.

Below program shows the way to use LinkedList.

Output:

Ravi

Ram

Ravi

Rajeev

 

        7.    Vector

Vector is a legacy class in collection framework. It is old and now been restructured. Vector is like ArrayList. It uses a dynamic array to store elements. The only difference Vector and ArrayList is a vector is Synchronized hence, it is lower in performance.

Below program show the usage of vector 

Output:

Ravi

Ram

Ravi

Rajeev

 

        8.    Stack

The stack is the subclass of the vector. It contains the methods of Vector class as well as its methods. There are 5 main methods of Stack.

·       push(element e) – use to add elements into the stack

·       boolean peek – looks for the top element.

·       boolean empty() – checks if the stack is empty or not

·       pop() – removes the top element of the stack

·       search(object) – searches for the specified element.

To understand the stack we can imagine a pile of coins. The pile of coins will also follow LIFO mechanism where whenever we want to add a coin we will add it to the top and for removal also we will remove it from the top. The last coin which was added will be removed first.


Below program shows the usage of Stack

Output:

Ravi

Ram

Ravi

Rajeev

 

        9.    Queue

Unlike stack, it follows the First In First Out i.e. FIFO mechanism. The queue is used in the scenario where we must process a large amount of data. In such cases, we create a queue of data that will be processed one by one.

The queue can be imagined as a line in front of a ticket counter where the people are waiting for their turn. The person who came first in the queue will be attended first and so on.

PriorityQueue and Dequeue are the two interfaces which extend the Queue interface. Queue interface can be instantiated in the following ways:

·       Queue<String> q1 = new PriorityQueue<String>();

·       Queue<String> q1 = new ArrayDeque<String>();

PriorityQueue and Deque interfaces are the two interfaces which implement Queue interface.


        10.  PriorityQueue

This class process the data based on their priority. The data which has a higher priority will be processed first. The PriorityQueue does not allow null values in the queue.

Below is the example:

 

        11.  Deque

Deque is operational from both sides. We can remove the elements as well as add the elements from the front as well as back. Deque is the acronym of Doubly Ended Queue.

Below is the way to instantiate deque

Deque d = new ArrayDeque();

 

        12.  ArrayDeque

ArrayDeque is the implementation of Deque Interface. Hence, we can add or remove the elements from both the ends. ArrayDeque has no capacity restrictions and is faster than ArrayList and Stack.

Below is the example of ArrayDeque

Output:

Kiran

Karan

Arjun

 

        13.  Set Interface  -

This interface is present in java.util package. It represents the unordered set of elements with no duplicates. It can hold at most one null value. The implementation classes are HashSet, LinkedHashSet and TreeSet.

In three ways it can be instantiated

Set<T> set1 = new HashSet<T>();
Set<T> set2 = new LinkedHashSet<T>();
Set<T> set3 = new TreeSet<T>();

 

        14.  HashSet

The class which implements set interface. This class uses the HashTable for storage. Hashing mechanism is used to store the elements into the HashSet. HashSet does not allow duplicated values. Hence, the scenario where we do not want duplicates to be inserted, we can use HashSet. For storing the elements, internally it uses HashMap which store the value as dummy “PRESENT” and key as the passes object.

The implementation example is given below:

Output:

Ram

Ravi

Rajeev

 

        15.  LinkedHashSet

LinkedHashSet implements set interface. It uses LinkedList internally for storing the data. LinkedHashSet extends HashSet class.

An implementation example is given below:

Output:

Ram

Ravi

Rajeev

 

        16.  SortedSet

If we want to take control of sorting of the elements or if we want to preserve the insertion order of the elements, then we should go for SortedSet. It follows the increasing order of the elements.

Way to instantiate the SortedSet

SortedSet<T> sortedSet1 = new TreeSet<T>();

 

        17.  TreeSet

TreeSet uses Tree data structure for storing the elements. As it implements Set interface, duplicates are not allowed. The sorting takes place in ascending order. Accessing the data is very fast in TreeSet.

Implementation Example is given below:

Output:

Ajay

Dev

Ram

Post a Comment

0 Comments