Java Collections Framework

This tutorial will introduce you to the Java Collections Framework. Java Collections Framework is one of the most important topics. Because you can do a lot of data. Java Collections Framework provides interfaces and classes to represent and manipulate the data. Java Collections Framework reduces the efforts you put in programming, there is a small learning curve once understood you are good to go.

You can use the Java Collections Framework when you have a huge data set to process. For example, you have a list of cities that you first need to sort in ascending or descending order then the sorted list will have to be displayed on the GUI. In this case, collections are very useful. You can use the different type of collection classes depending on the use case. Follow the tutorial to understand the use cases of each type of collection with the example.

Before proceeding with this tutorial let’s acknowledge the confusion that people have around this topic.

  • Newbie to Java Collections Framework generally gets confused in calling it “Java Collection Framework” or “Java Collections Framework”. It is written as Java Collections Framework. It has Collection Interface and Collections Class. Collection Interface is the root interface of the Collections framework. Which means every single class in Collections Framework implements Collection interface directly or indirectly. And, Collections Class is a utility class contains static methods for doing operations on objects of classes which implements the Collection interface.
  • The second confusion is regarding Map, “whether it is a part of Collections Framework or not?” Map is an interface which does not extents the Collection interface so it is not a true collection.  However, these interfaces contain collection-view operations, which enable them to be manipulated as collections. Hence, studying under Java Collections Framework.
Concrete Class/Map  Interface  Duplicate   Elements  Ordered Sorted   Allow Null?
ArrayList List Yes By Index No Yes
Vector List Yes By Index No Yes
LinkedList List, Queue Yes By Index No Yes
HashSet Set No No Order No Yes
LinkedHashSet Set No By Insertion Order No Yes
TreeSet SortedSet No Sorted Natural/Custom No
HashMap Map unique keys No Order No Yes
HashTable Map unique keys No Order No No
LinkedHashMap Map unique keys By Insertion Order or Last Accessed No Yes
TreeMap SortedMap unique keys Key Order Natural/Custom No
PriorityQueue Queue Yes Natural Ordering Natural Ordering No

Let’s get started with the understanding of Java Collections Framework with the quick visual.

The diagram below is the extended version of Java Collections Framework but doesn’t show all the interfaces (like RandomAccess, Cloneable, Serializable) so that it is easy to understand at the same time give a clear picture of what the hierarchy looks like.

Interfaces, Abstract classes, and Classes are marked clearly. The classes are in blue and provide the necessary methods for programming.


Java Collections Framework: As its name suggests, it is a collection, collection of objects called elements. A collection can be ordered or unordered some collections allow duplicate and some are not. We will see

Collection Interface: You can not use the Collection Interface directly in the code. Instead, it has been extended by List, Set, and Queue.

List Interface: List is ordered means preserve the order, but not sorted. It allows duplicate elements and null as well.Elements can be accessed by using the index number, indexing starts from zero.

Set Interface: Set doesn’t allow duplicates elements. But other features can vary depending on the implementing class like HashSet doesn’t preserve order but LinkedHashSet and TreeSet does.

Queue Interface: A Java Queue is a collection that holds elements before performing any operation. Here Objects are added to the tail and removed from the head and the object added first will be removed first.

Map Interface: Map, as said does not extend Collection Interface so represent separately. Boxes in blue are the classes that are used for programming. It contains Key-Value pair. Key will be unique. No class extending Map preserves the order accept TreeMap class



Download Examples from GitHub

Download Examples from codedbug GitHub repo 


Most Frequently Asked Questions On Java Collection

A collection is a topmost interface in the java collection hierarchy while Collections is a utility class.
whenever we create a HashSet, it internally creates a HashMap and when we insert an element into this HashSet using add() method here it actually calls put() method on internally created HashMap object with an element you have specified as it’s key and constant Object called “PRESENT” as it’s value. So we can say that a Set achieves uniqueness internally through HashMap.
It is a unique collection type which not only stores elements but also supports flow control by introducing blocking if either BlockingQueue is full or empty.
Fail-fast iterator Throw ConcurrentModification Exception while for Fail-safe iterator it is "NO". Fail-fast iterator doesn't clone object while Fail-safe iterator does. Example of fail-fast iterator is HashMap, Vector, ArrayList, HashSet Example of Fail-safe iterator is CopyOnWriteArrayList, ConcurrentHashMap
You can create read-only Collection by using Collections.unmodifiableCollection() utility method. it returns an unmodifiable or read-only view of Collection in which you can not perform any operation
No, Set Interface does allow null value only its implementation i.e. TreeSet doesn't allow null value because it stores element in sorted order so every time one adds a new element it compares value and then it sorts. so internally what happens is it compares the new added null value with existing values, so it throws NullPointerException.