Java Collection Framework

Krishna Bankar
6 min readFeb 14, 2021

--

Set, List, Queue are Collection Interfaces.Collection Interface extends Iterator.

  1. Introduction
  2. Collection Framework
    i. List
    — a. ArrayList
    — b. Stack
    — c. Vector
    — d. LinkedList
    ii. Set
    — a. HashSet
    — b. LinkedHashSet
    — c. TreeSet
    iii. Queue
    — a. PriorityQueue
    — b. ArrayDeque
    — c. LinkedList
  3. Map
    i. HashMap
    ii. HashTable
    iii. LinkedHashMap
    iv. TreeMap
  4. Conclusion
  5. References

Introduction :

The Collection in Java is a framework that provides an architecture / DataStructure to store and manipulate the group of objects. Java Collection framework provides many interfaces (Set, List, Queue, Deque) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet). Collection is an interface which extends Iterable interface, all Collection related Interfaces and Classes contains inside java.util. package.

2. Collection Framework :

As we know Collection contains many Interfaces and Classes Let’s have look at some Collection Interfaces and Classes :

i. List :
— java.util.List;
— List is an Interfaces It extends Collection interface.
— It stores a sequence of elements, and can be accessed by their position in the list, using a zero-based index.
— List maintains insertion order and does not contains duplicate element.
— List implementation classes are : ArrayList, Stack, Vector and LinkedList.

a. ArrayList :
— java.util.ArrayList;
— ArrayList is an implementation class of List interface.
— List list = new ArrayList();
— built-in methods like : add, get, set, remove, index, iterator, contains, sort and many more.

b. Stack :
— java.util.Stack;
— List stack = new Stack();
— Stack is the data structure follow last in fist out (LIFO).
— already built-in methods are to use stack funcitonality like push, pop, top, peek, search, empty.

c. Vector :
— java.util.Vector;
— List vector = new Vector();
— vector is legacy implementation class of List which contains all synchonized methods.
— vector works same as Stack but Vector is thread safe.
— at a time only one thread can access Vector object.

d. LinkedList :
— java.util.LinkedList.
— List link = new LinkedList();
— LinkedList is an implementation class of List.
— LinkedList follow link list data structure to store and acces elements.
— It can works like and Stack or abstract Stack.
— It can accessed through head, tail, next pointer.

ii. Set :
— java.util.Set;
— Set is an Interface and extends Collection interface.
— Set is an interface and HashSet, LinkedHashSet, TreeSet, EnumSet are implementation classes.
— It is also used to store objects but behaviour is different than List.
— Set does not contains duplicate element and does not maintain any order.

a. HashSet :
— java.util.HashSet.
— Set hset = new HashSet();
— HashSet is an implementation class of Set Interface.
— HashSet does not maintains insertion order like List but arrage object using hashing technic.
— built-in methods : add, remove, contains, retains, iterator, clone, clear etc.
— HashSet mostly use for set, unioun, intersection, sebset etc operation.

b. LinkedHashSet :
— java.util.LinkedHashSet;
— Set lhset = new LinkedHashSet();
— LinkedHashSet implements Set interface and extends HashSet class.
— LinkedHashSet works same as HashSet but It follows link list data structure to store and access object / element.

c. TreeSet :
— java.util.TreeSet;
— Set tset = new TreeSet();
— TreeSet is implementation class of NavigableSet interface and NavigableSet interface is child of
SortedSet interface and SortedSet is child interface of Set interface.
— TreeSet stores object in sorted order and also follows Tree data structure like root , left-child and right-child.

iii. Queue :
— import java.util.Queue;
— Queue is an Interface and its implementation class is PriorityQueue.
— BlockingQueue and Deque are child interfaces of Queue.
— TransferQueue is child interface of BlockingQueue and LinkedTransferQueue is implementation class of TransferQueue;
— BlockingQueue implementation classes are ArrayBlockingQueue, LinkedBlockingQueue, PriorityBlockingQueue, SyschronousQueue, DelayQueue.
— Deque is an interface, BlockingDeque is child interface and ArrayDeque is implementation class.

a. PriorityQueue :
— java.util.PriorityQueue;
— Queue que = new PriorityQueue();
— PriorityQueue follows queue data structure first in first out (FIFO).

b. ArrayDeque :
— java.util.ArrayDeque;
— Queue que = new ArrayDeque();
— ArrayDeque is an abstract queue data structure.
— we can add element and remove element from both side front and rear.

c. LinkedList :
— Queue que = new LinkedList();
— LinkedList is also implements Queue interface and perform queue oprations.
— in LinkedList data structure of Queue maintains head as fron and tail as rear.
— LinkedList can also perform operations like ArrayDeque.

3. Map :

— However Map is not a Collection framework part but we always study Map with Collection.
— Map is an interface.
— import java.util.Map;
— Map is an interface and HashMap, LinkedHashMap, TreeMap, HashTable are implementation classes.
— LinkedHashMap implement Map interface and extends HashMap class;
— TreeMap implement NavigableMap and NavigableMap extends SortedMap and SortedMap extends Map;
— HashMap extends AbstractMap abstract class and HashTable extends Dictionary abstract class.

i. HashMap :
— java.util.HashMap;
— HashMap is implement class of Map interface.
— Map<k,v> map = new HashMap<>();
— Map used to store object as key and value pair.
— hashing technic is used to store objects.
— HashMap contains many methods like : put, get, replace, remove, contains, compute etc.

ii. LinkedHashMap :
— java.util.LinkedHashMap;
— Map<k,v> map = new LinkedHashMap<>();
— LinkedHashMap same as HashMap but it implements link list data structure.

iii. HashTable :
— java.util.HashTable;
— Map<k,v> map = new HashTable<>();
— HashTable implements Map interface and extends abstract Dictionary class.
— HashTable works same as HashMap but difference is HashTable is legacy class.
— HashTable contains all synchonized methods and at a time only one thread can acces HashTable object.

iv. TreeMap:
— java.util.TreeMap;
— Map<k,v> map = new TreeMap<>();
— TreeMap implement NavigableMap and NavigableMap extends SortedMap and SortedMap extends Map;
— behaviour of TreeMap is sorted objects and store objects in key-value pair.
— used Tree data structure root, left-child, and right-child.

4. Conclusion :

Collection is an Interface which extends Iterable interface. Collection has Set, List, Queue interfaces. For each Collection interface there are many implementation classes for different purposes. Map is an interface that we usely study with Collection but Map is not Collection interface. Collection implementation classes stores objects as single entity where Map stores data in key-value value. There is Collections class in java.util.*; package. used to give flexiblity to Collection implementation classes and contains Collection spacific methods.

5. References :
References for further reading :
1. https://www.javatpoint.com/collections-in-java
2.
https://www.geeksforgeeks.org/collections-in-java-2/
3.
https://www.tutorialspoint.com/java/java_collections.htm
4.
https://www.programiz.com/java-programming/collections
5.
https://docs.oracle.com/javase/tutorial/collections/index.html

--

--

Krishna Bankar
Krishna Bankar

Written by Krishna Bankar

Trainne at MountBlue Technology

No responses yet