Java Collection framework overview

In this article, we will discuss on Java collection framework (JCF) overview and quick pointers about each important collection classes

Java Collection framework hierarchy

1-Java-collection-framework-hierarchy-overview

Source: Team BenchResources.Net

ArrayList (java.util.ArrayList)

  • extends java.util.AbstractList and implements java.util.List
  • uses dynamic array to store elements inside ArrayList
  • size increases by 50% of current array size when ArrayList exceeds its capacity
  • allows to insert/add duplicate elements
  • maintains insertion order
  • accessing elements is faster (as we can use index to access any elements)
  • manipulation i.e.; delete is very slow due to shifting of elements
  • ArrayList is non-synchronized
  • Read more about ArrayList class

LinkedList (java.util.LinkedList)

  • extends java.util.AbstractList and implements java.util.List & java.util.Deque
  • uses dynamic array to store elements inside LinkedList
  • allows to insert/add duplicate elements
  • maintains insertion order
  • manipulation i.e.; delete is very faster as no shifting is required (the links break and new link is formed while deleting any element from LinkedList)
  • LinkedList is non-synchronized
  • Read more about LinkedList class and
  • difference between ArrayList v/s LinkedList

Vector (java.util.Vector)

  • extends java.util.AbstractList and implements java.util.List & java.util.Deque
  • uses grow-able array to store elements
  • size increases by 100% of current array size when Vector exceeds its capacity
  • allows to insert/add duplicate elements
  • maintains insertion order
  • Vector is legacy and it is synchronized
  • Read more about Vector class

Vector v/s ArrayList:

Vector ArrayList
Vector is legacy (including other 4 viz., HashTable, Stack, Dictionary, Properties) ArrayList is introduced in Java 1.2
All legacy collection classes are synchronized, thus Vector is synchronized  ArrayList is not synchronized (need to make sure while working in multi-threaded environment)
Performance-wise vector is slower comparing with ArrayList due to synchronization This is comparatively faster as it is non-synchronized
Vector increases its size by 100% of current array when its capacity exceeds ArrayList increases its size by 50% of current array when its capacity exceeds
Both Iterator & Enumeration can be used to iterate items/elements inside Vector Only Iterator is allowed to iterate items/elements inside ArrayList

Read more about ArrayList v/s Vector

HashSet (java.util.HashSet)

  • extends java.util.AbstractSet and implements java.util.Set
  • uses hashtable to store elements inside HashSet
  • allows only unique elements (doesn’t allow duplicates, if any duplicate is encountered then it is overridden)
  • insertion order is not maintained i.e.; items/elements order is random while iterating
  • permits null element
  • HashSet is non-synchronized
  • But it can be easily synchronized using Collections class i.e.; Collections.synchronizedSet(hashSet)
  • HashSet is fail-fast –> throws ConcurrentModificationException is modified by any other means than HashSet’s own remove() method
  • Read more about HashSet class

LinkedHashSet (java.util.LinkedHashSet)

  • extends java.util.HashSet and implements java.util.Set
  • allows only unique elements (doesn’t allow duplicates, if any duplicate is encountered then it is overridden)
  • allows null element (if more than one is inserted, still it will contain only one null element)
  • insertion order is maintained (in the order they are inserted)
  • Read more about LinkedHashSet class and
  • difference between HashSet v/s LinkedHashSet

TreeSet (java.util.TreeSet)

  • extends java.util.SortedSet and implements java.util.NavigableSet
  • allows only unique elements (doesn’t allow duplicates, if any duplicate is encountered then it is overridden)
  • allows null element (if more than one is inserted, still it will contain only one null element)
  • It sort the elements and maintains ascending order
  • Read more about TreeSet class

List v/s Set:

  • List allows duplicate items/elements
  • Set allows only unique items/elements (if same element inserted again, still it will be contain just one element with that value)
  • Read more about List v/s Set

PriorityQueue (java.util.PriorityQueue)

  • Maintains FIFO order i.e.; orders in the PriorityQueue are in FIFO fashion
  • FIFO –> First-In First-Out
  • Important methods of PriorityQueue is poll(), peek(), remove(), head()
  • Read more about PriorityQueue class

HashMap (java.util.HashMap)

  • extends java.util.AbstractMap and implements java.util.Map
  • HashMap stores key\value pairs
  • allows only unique elements
  • maximum of only-one null key is allowed (but it can have any number of null values corresponding to key)
  • maintains no order
  • HashMap is not synchronized
  • But it can be easily synchronized using Collections class i.e.; Collections.synchronizedMap(hashMap)
  • Read more about HashMap class

LinkedHashMap (java.util.LinkedHashMap)

  • extends java.util.HashMap and implements java.util.Map
  • LinkedHashMap stores key\value pairs
  • allows only unique elements
  • maximum of only-one null key is allowed (but it can have any number of null values corresponding to key)
  • maintains insertion order (based on Key)
  • LinkedHashMap is not synchronized
  • Note: very similar to HashMap except maintaining insertion order
  • Read more about LinkedHashMap class and
  • difference between HashMap v/s LinkedHashMap

TreeMap (java.util.TreeMap)

  • extends java.util.AbstractMap and implements java.util.NavigableMap
  • TreeMap stores key\value pair
  • allows only unique elements
  • Null key is not allowed (but it can have any number of null values corresponding to key)
  • maintains ascending order (based on Key)
  • TreeMap is not synchronized
  • Note: very similar to LinkedHashMap except maintaining ascending order
  • Read more about TreeMap class and
  • difference between HashMap v/s LinkedHashMap v/s TreeMap

Hashtable (java.util.Hashtable)

  • extends java.util.Dictionary and implements java.util.Map
  • HashTable stores key\value pairs
  • allows only unique elements
  • Null key is not allowed and also null value is not allowed
  • HashTable is synchronized i.e.; thread-safe
  • Doesn’t maintain order (while iterating)
  • Read more about Hashtable class

HashTable v/s HaspMap:

HashTable HashMap 
HashTable is legacy (including other 4 viz., Vector, Stack, Dictionary, Properties) HashMap is introduced in Java 1.2
All legacy collection classes are synchronized, thus HashTable is synchronized  HashMap is not synchronized (need to make sure while working in multi-threaded environment)
 Performance-wise HashTable is slower comparing with HashMap due to synchronization  This is comparatively faster, as it is non-synchronized
HashTable neither allows null key or null values  HashMap allows only one null key (although it allows multiple null values corresponding to key)
 Both Iterator & Enumeration can be used to iterate over HashTable items/elements Only Iterator is allowed to iterate over HashMap items/elements

Read more about difference between HashMap v/s Hashtable

Collection Sorting

We should always check 2 important interfaces while dealing collection framework in Java. Those are

  1. Comparable
  2. Comparator

Comparable v/s Comparator:

Comparable  Comparator
Comparable belong to java.lang package Comparator belong to java.util package
Provides compareTo() method to sort items/elements

Signature: public int compareTo(T o);

Provides compare() method to sort items/elements

Signature: public int compare(T o1, T o2);

 This is used for single-sorting over the collection items/elements  This is used for multiple-sorting over the collection items/elements
Original class needs to be implement java.lang.Comparable interface

Thus affecting original class

A separate new class implements java.lang.Comparator interface

Thus, doesn’t affects original class

Static method “sort(List)” of Collections class is used to sort collection items/elements

Signature: Collections.sort(List lst);

Static method “sort(List, Comparator)” of Collections class is used to sort collection items/elements

Signature: Collections.sort(List lst, Comparator cmp);

Read more about

 

Read also:

 

Happy Coding !!
Happy Learning !!

Interview program on String using toString() method
Interview Questions and Answers on Java JDBC