Java Collections Framework

Java collections framework help us to work with collection of objects effectively and apply different options such as sorting, searching, insertion, deletion and update.

Collections framework consists of interfaces, classes and their implementations.

Collection framework got introduced in JDK 2.1. It contains multiple interfaces and classes.

Collection interface is the root interface of java.util.Collection and java.util.map.

java.util.Collection interface is a parent interface of List, Queue and set.

java.util.map is a parent interface of HashMap, TreeMap, LinkedHashMap and Hashtable classes.

List

List is an interface belongs to java.util package and it is a child interface of Collection interface.

Elements maintain insertion order and can be access by using index. List items can be accessed in both forward and backward direction.

List allows multiple duplicate values as well.

ArrayList:
ArrayList is a class which implements List interface.

It is a better version of an Array where it internally uses array to story elements and provides us the flexibility to resize the list size internally after adding new element.

In ArrayList all elements store in contiguous locations like arrays. ArrayList maintains the insertion order of elements.

LinkedList:

LinkedList is all similar to arrays except the elements don’t get store in contiguous locations. All elements are linked to each other using memory reference.

Each LinkedList element will have a reference of next LinkedList element.

LinkedList can acts as both list and queue as it implements List and Queue interface.

Vector:

Vector is a class which implements List interface.

It is a better version of an Array where it internally uses array to story elements and provides us the flexibility to resize the list size internally after adding new element.

In Vector all elements store in contiguous locations like arrays. Like ArrayList, vector also maintains insertion order.

Map

Map is an interface that belongs to java.util package and stores data as key value pair.

It allows single null key and multiple null values.

It doesn’t maintain any insertion order.

HashMap:

HashMap is a class which implements Map interface.

Map collection used to store key value pair. It doesn’t maintain insertion order which means there is no guarantee for the order of key value pair while accessing the HashMap.

TreeMap:

TreeMap is a Red-Black tree based implementation.

It extends AbstractMap class and implements NavigableMap interface which internally implements SortedMap interface.

The map by default get sorted based on keys.

The sorting totally depends on the constructor we used to create TreeMap.

LinkedHashMap:

LinkedHashMap is similar to HashMap with an additional feature of maintaining insertion order.

It allows us to quick insert, update and delete of elements like HashMap.

It can have only one null key and multiple null values like of HashMap.

Hashtable:

Hashtable class extends Dictionary class and implements Map interface.

It is almost like HashMap with major two differences. First, it doesn’t allow null key and value on adding the same will throw NullPointerException. Second, HashTable is synchronized which means only one thread can access at the same time.

Set

Set is an interface that belongs to java.util package.

It allows only single null value as well as unique value to store.

It doesn’t maintain any insertion order.

HashSet:

HashaSet class implements Set interface. HashSet allows only unique values with random insertion order.

Notes:

  1. HashSet is not synchronized.
  2. HashSet doesn’t allow to store duplicate values doing so will override the last value.
  3. HashSet elements doesn’t maintain any order and return random order.
  4. HashSet allows only single null value to store and it will override existing null value on adding further null values.

LinkedHashSet:

LinkedHashSet extends HashSet class and implements Set interface.

It maintains the insertion order. LinkedHashSet stores only unique entries like HashSet.

TreeSet:

TreeSet is similar to HashSet except it sorts the TreeSet entries in ascending order.

It implements NavigableSet interface which internally extends SortedSet interface.

TreeSet class is not synchronized.

HashSet: doesn’t maintain any order.

TreeSet: sort elements in ascending order.

LinkedHashSet: maintains the insertion order.

Let’s discuss every topic in subsequent chapters with complete details.

Imran Khan

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

📝 Blogs

📧 Imran Khan