Saturday, January 11, 2020

Java Collections Framework

Java Collections Framework


A collection can be considered as a container which is simply an object and puts multiple elements in a group to constitute a single unit. We can store, retrieve, manipulate, and apply some common actions to this group of elements kept in a single unit.

What is the Collections Framework?

The collections framework is constructed with several interfaces, algorithms, and implementations to provide a unified architecture to manipulate and access the elements grouped in various types of Collection objects.

Why should we use the Collections Framework?

The main advantages of using the Collections framework are,
  • To enhance the quality and speed creating a program
  • Easy to learn and apply
  • Reducing programming effort

Who is in the Collections Framework?

At the top of the class hierarchy, There are four main interfaces under the Collection interface,

  1. Set-implemented by classes like HashSet, TreeSet, LinkedHashSet
  2. List-implemented by classes like ArrayList, LinkedLists, Vector
  3. Queue-implemented by classes PrioriyQueue.
  4. Deque 

Map Interfaces(implemented by classes like HashMap and TreeMap etc.) and all its implementing interfaces are also considered to be the members of the Collections framework, but they are not true collections.

The Collection Interface

A collection represents a group of objects known as its elements. The Collection interface is the minimum common behavior that all types of collections must implement and is used to pass collections around and to manipulate them when maximum generality is desired. Some types of collections allow duplicate elements like Lists and few do not allow like Sets. Some can be ordered and some unordered. 

The Members of Collections Framework


Set is a collection that cannot contain duplicate elements. This interface models the mathematical set abstraction and is used to represent sets, such as the cards comprising a poker hand, the courses making up a student's schedule, or the processes running on a machine. 


List is an ordered collection and can contain duplicate elements. The user of a List generally has precise control over where in the list each element is inserted and can access elements by their index or position.


Queue is a collection used to hold multiple elements prior to processing. Besides basic Collection operations, a Queue provides additional insertion, extraction, and inspection operations.


Deques are like queues except they can be used both as FIFO (first-in, first-out) and LIFO (last-in, first-out). In a Deque all new elements can be inserted, retrieved and removed at both ends. 


Map is an object that maps keys to values. A Map cannot contain duplicate keys. It means each key can map to at most one value.

Larger Picture Of The Class Hierarchy

In the figure above, green blocks are interfaces and red blocks are Classes.

Video Tutorial