Collection Framework In Java

Nishajha
8 min readFeb 15, 2021

Collection Framework:

It is a framework that is used to store data and to perform data operation.Collection Framework has many Interfaces,classes and utility methods which are present in java.util package.

Features Of Collection Framework:

  1. Collection is heterogeneous i.e. it can store different types of data.
  2. Collection is dynamic or elastic i.e. we can add or remove data dynamically.
  3. Collection Framework has many utility methods to deal with data.
  4. Collection framework classes uses data structure.
  5. Collection can store non-primitive data.
Collection Framework Hierarchy

Collection framework has mainly two pillars-

  1. Collection Interface
  2. Map Interface

Collection Interface

java.util.Collection is one of the supermost interface of Collection framework. The Collection interface builds the foundation for the Collection framework. The collection interface is one of the interfaces which is implemented by all the Collection framework classes. It provides common methods to implement by all the subclasses of collection interfaces.

Autoboxing with Collection:

Collection cannot store primitive data,but when we try to store primitive data it will not give any compilation error because primitive data gets autoboxed to its corresponding wrapper Object and wrapper Object gets stored in the Colllection.

List Interface

List interface is the subtype/child interface of the Collection interface present since jdk 1.2. It stores objects/elements in list type data structure.

  1. List can index-based and can store duplicate value.
  2. List maintains insertion order.

Implementation classes for List interface are ArrayList, LinkedList, Vector, and Stack.

List  list1= new ArrayList();  
List list2 = new LinkedList();
List list3 = new Vector();
List list4 = new Stack();

ArrayList

The ArrayList implements the List interface present since jdk 1.2. ArrayList internally stores data in the form of array.Initial capacity of array is 10 and the incremental capacity is (current capacity*3)/2+1.

It’s having the following features:

  • ArrayList uses a dynamic array data structure to store objects and elements.
  • ArrayList allows duplicate objects and elements.
  • ArrayList maintains the insertion order.
  • ArrayList is non-synchronized.
  • ArrayList elements/objects can be accessed randomly.
  • The internal implementation data structure is growable/resizeable.
  • ArrayList implements Marker interfaces like Serializable,Cloneable,RandomAccess.
ArrayList Example

Situations to use ArrayList:

  1. ArrayList is good choice for data retrieval and search operation.
  2. Time taken to retrieve any data randomly from an ArrayList is same.

Situations not to use ArrayList:

  1. ArrayList is not a good choice to insert or remove the data in between because of shift operations(more shift operations reduces the performance).

LinkedList

LinkedList implements the List interface. It’s having the following features:

  • LinkedList uses a doubly linked list data structure to store elements.
  • LinkedList allowed storing the duplicate elements.
  • LinkedList maintains the insertion order.
  • LinkedList is not synchronized.
  • LinkedList manipulation is fast because no shifting is required.
  • LinkedList stores the data in the form of nodes ,where each node is connected to next and previous node.
  • LinkedList implements marker Interface like Serializable,Cloneable but not RandomAccess.
LinkedList Example

Situations to use LinkedList:

It is a good choice for adding or removing the data in between beacuase there is no shift operations.

Situations not to use LinkedList:

LinkedList is not a good choice for data retrieval or search operation because retrieval or search operation always has to be start from 1st node.

Vector Class

Vector Class implements List interface. It is a legacy class present since jdk 1.2.It’s having the following features:

  • Vector is similar to the ArrayList class.
  • Vector class uses data structure as a dynamic array to store the data elements.
  • Vector is synchronized.
  • Vector contains many methods that are not the part of Collection Framework.
  • Vector implements Marker Interface like Serializeable,Cloneable, and RandomAccess.
Vector Example

Stack Class

The Stack is the subclass of the Vector class. It’s having the following features:

  • Stack implements the Vector data structure with the (LIFO)last-in-first-out.
  • Stack contains all of the methods of the Vector class.
  • Stack also provides its methods like boolean push(), boolean peek(), boolean push(object o), which defines its features.
Stack Example

Queue Class

Queue Interface extends the Collection interface. It’s having the following features:

  • Queue interface maintains the FIFO (first-in-first-out) order.
  • Queue can be defined as an ordered list that is used to hold the elements which are about to be processed.
  • Queue interface implemented by the various classes like PriorityQueue, Deque, and ArrayDeque.

Queue interface can be instantiated as:

Queue q1 = new PriorityQueue();  
Queue q2 = new ArrayDeque();

PriorityQueue

The PriorityQueue class implements the Queue interface.

  • PriorityQueue holds the elements or objects which are to be processed by their priorities.
    PriorityQueue doesn’t allow null values to be stored in the queue.
PriorityQueue

Deque Interface

Deque stands for the double-ended queue which allows us to perform the operations at both ends.interface extends the Queue interface.

  • Deque extends the Queue interface.
  • Deque allows remove and add the elements from both the side.
Deque d = new ArrayDeque();

ArrayDeque

ArrayDeque class implements the Deque interface.

  • ArrayDeque facilitates us to use the Deque.
  • ArrayDeque allows add or delete the elements from both the ends.
  • ArrayDeque is faster than ArrayList and has no capacity restrictions.

Set Interface

Set Interface extends Collection Interface and present in java.util package.

  • Set doesn’t allow duplicate elements or objects.
  • Set store elements in an unordered way.
  • Set allows only one null value.
  • Set is implemented by HashSet, LinkedHashSet, and TreeSet.

We can create an instantiation of Set as below:

Set s1 = new HashSet();  
Set s2 = new LinkedHashSet();
Set s3 = new TreeSet();

i.HashSet

HashSet class implements Set Interface. It’s having the following features:

  • HashSet internally uses data structure like a hash table for storage.
  • HashSet uses hashing technique for storage of the elements.
  • HashSet always contains unique items.
  • HashSet is heterogeneous and can store single null element.
  • HashSet implements Serializable,Cloneable but not RandomAccess.
HashSet Example

ii.LinkedHashSet

LinkedHashSet class implements Set Interface present since jdk 1.4. It’s having the following features:

  • LinkedHashSet store items in LinkedList.
  • LinkedHashSet store unique elements.
  • LinkedHashSet maintains the insertion order.
  • LinkedHashSet allows null elements.
  • Data structure used is Hash table and LinkedList.
LinkedHashSet Example

iii.SortedSetInterface

SortedSet Interface extends Set Interface. It’s having the following features:

  • SortedSet provides a total ordering on its elements.
  • SortedSet elements are arranged in the increasing (ascending) order.
  • SortedSet provides additional methods that inhibit the natural ordering of the elements.

The SortedSet can be instantiated as:

SortedSet set = new TreeSet();

TreeSet Class

TreeSet class implements the SortedSet interface. It’s having the following features:

  • TreeSet uses a tree data structure for storage.
  • TreeSet also contains unique elements.
  • TreeSet elements access and retrieval time is quite fast.
  • TreeSet elements stored in ascending order.
  • TreeSet uses default natural sorting order i.e. Ascending order.
  • TreeSet is homogeneous.
  • TreeSet cannot even store single null element.
  • TreeSet automatically/internally uses comparable.Comparator is used for custom sorting.
TreeSet

Map Interface

In the collection framework, a map contains values on the basis of key and value pair. Map is separate vertical of Collection package present in java.util package.This pair is known as an entry. A map having the following features:

  • Map contains unique keys.
  • Map allows duplicate values.
  • Map is useful to search, update or delete elements on the basis of a key.
  • Map is the root interface in Map hierarchy for Collection Framework.
  • Map interface is extended by SortedMap and implemented by HashMap, LinkedHashMap.
  • Map implementation classes HashMap and LinkedHashMap allow null keys and values but TreeMap doesn’t allow null key and value.
  • Map can’t be traversed, for traversing needs to convert into the set using method keySet() or entrySet().
  • Entry is a data in the form of key value pair associated data.In Entry key must be unique but value can be duplicated or even can be null.

i.HashMap Class

HashMap class implements Map interface. It’s having following features:

  • HashMap uses data structure as a Hash Table.
  • HashMap store values based on keys.
  • HashMap contains unique keys.
  • HashMap allows duplicate values.
  • HashMap doesn’t maintain order.
  • HashMap class allows only one null key and multiple null values.
  • HashMap is not synchronized.
  • HashMap initial default capacity is 16 elements with a load factor of 0.75.
HashMap Example

ii.LinkedHashMap

LinkedHashMap class extends the HashMap class. It’s having the following features:

  • LinkedHashMap contains values based on the key.
  • LinkedHashMap contains unique elements.
  • LinkedHashMap may have one null key and multiple null values.
  • LinkedHashMap is not synchronized.
  • LinkedHashMap maintains the insertion order.
  • LinkedHashMap default initial capacity is 16 with a load factor of 0.75.
LinkedHashMap

iii.TreeMap

TreeMap class implements the SortedMap interface. it’s having the following features:

  • TreeMap uses data structure as red-black tree.
  • TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class.
  • TreeMap contains only unique elements.
  • TreeMap doesn’t allow null keys and values and it is homogeneous.
  • TreeMap is not synchronized.
  • TreeMap maintains an ascending order.
  • TreeMap is mainly used for sorting.Sorting is based on on the key.
TreeMap

Iterable Interface:

The Iterable interface is the root interface for all the collection classes because the Collection interface extends the Iterable interface, therefore, all the subclasses of Collection interface also implement the Iterable interface.

The iterable interface contains only one abstract method.

  • Iterator iterator(): It returns the iterator over the elements of type T.

Iterator:

  1. It is a cursor which is used to iterate the data from collection.
  2. Programmatically,Iterator is an interface present java.util package.

The important methods of Iterator are:

  1. public boolean hasNext():It checks whether the next element is present or not,if element is present the method returns true otherwise it returns false.
  2. public E next():It is used to move the cursor from one element to next element and also to fetch and retrieve data.
  3. public void remove():It is used to remove all data while iteration.

--

--