Collections Framework in Java

Framework

surya mdl
4 min readFeb 15, 2021

A framework is a set of classes and interfaces which provide a ready-made architecture.

Let us have a look at the collection framework in java

Interfaces in Collection Framework

Collection Interface

The Collection interface is the interface which is implemented by all the classes in the collection framework. It declares the methods that every collection will have. In other words, we can say that the Collection interface builds the foundation on which the collection framework depends.

List Interface

List interface is the child interface of Collection interface. It inhibits a list type data structure in which we can store the ordered collection of objects. It can have duplicate values.

List interface is implemented by the

> ArrayList Class

> LinkedList Class

> Vector Class

> Stack Class

We can use list interface as reference for creating objects for these classes.

List <data-type> list1= new ArrayList();List <data-type> list2 = new LinkedList();List <data-type> list3 = new Vector();List <data-type> list4 = new Stack();

Queue Interface

Queue interface maintains the first-in-first-out order. It can be defined as an ordered list that is used to hold the elements which are about to be processed.The Queue interface is implemented by

> PriorityQueue Class

> Deque Class

> ArrayDeque Class

We can use queue interface as reference for creating objects for the above classes

Queue<String> q1 = new PriorityQueue();Queue<String> q2 = new ArrayDeque();

Deque Interface

Deque interface extends the Queue interface. In Deque, we can remove and add the elements from both the side. Deque stands for a double-ended queue which enables us to perform the operations at both the ends.

This interface is implemented by ArrayDeque class.

Deque d = new ArrayDeque();

Set Interface

Set Interface in Java is present in java.util package. It extends the Collection interface. It represents the unordered set of elements which doesn’t allow us to store the duplicate items. We can store at most one null value in Set.

Set is implemented by

> HashSet

> LinkedHashSet

> TreeSet.

Set can be instantiated as:

Set<data-type> s1 = new HashSet<data-type>();Set<data-type> s2 = new LinkedHashSet<data-type>();Set<data-type> s3 = new TreeSet<data-type>();

Comaparable Interface

Comparable interface in java is used to order the objects of user-defined class.This interface is found in java.lang package and contains only one method named compareTo(Object). The classes which implement this interface has to define compareTo(Object) method.

Comaparator Interface

Comparator Interface in java is used to order the objects of user-defined class.

This interface is found in java.util package.It contains compare(Object1,Object2) method.The classes which implements this interface has to define this method.

Classes in Collection Framework

ArrayList

Java ArrayList class uses a dynamic array for storing the elements. It is like an array, but there is no size limit. We can add or remove elements anytime. So, it is much more flexible than the traditional array. It is found in the java.util package.

The ArrayList in Java can have the duplicate elements also. It implements the List interface so we can use all the methods of List interface here. The ArrayList maintains the insertion order internally.

LinkedList

Java LinkedList class uses a doubly linked list to store the elements. It provides a linked-list data structure. It inherits the AbstractList class and implements List and Deque interfaces.

Hierarchy Of Linked List Class

Hash Set Class

Java HashSet class is used to create a collection that uses a hash table for storage. It inherits the AbstractSet class and implements Set interface.

The difference between list and set is that list stores duplicate values whereas set stores only unique values.

import java.util.*;class HashSet1{public static void main(String args[]){//Creating HashSet and adding elementsHashSet<String> set=new HashSet();set.add(“One”);set.add(“Two”);set.add(“Three”);set.add(“Four”);set.add(“Five”);Iterator<String> i=set.iterator();while(i.hasNext()){System.out.println(i.next());}}}
  • Linked hash set class preserves the insertion order of objects in it.
  • Tree hash set class sorts the objects stored in it in ascending order.

Hash Map

Java HashMap class implements the Map interface which allows us to store key and value pair, where keys should be unique. If you try to insert the duplicate key, it will replace the element of the corresponding key. It is easy to perform operations using the key index like updation, deletion, etc. HashMap class is found in the java.util package.

import java.util.*;public class HashMapExample1{public static void main(String args[]){HashMap<Integer,String> map=new HashMap<Integer,String>();//Creating HashMapmap.put(1,”Mango”); //Put elements in Mapmap.put(2,”Apple”);map.put(3,”Banana”);map.put(4,”Grapes”);System.out.println(“Iterating Hashmap…”);for(Map.Entry m : map.entrySet()){System.out.println(m.getKey()+” “+m.getValue());}}}

-> Linked Hash Map class is used to store the key and value pair where insertion order is preserved.

-> Tree Hash Map class is used to store the key and value pairs in sorted order according to keys.

--

--