Java Collections Framework

Thanoshan MV
3 min readJun 8, 2020

--

In this article we shall see what actually is Java Collections framework. We shall walk through Oracle Java Documentation to find out what it is!

What are Collections?

A collection — sometimes called a container — is simply an object that groups multiple elements into a single unit. — Oracle Java Documentation

Real world examples of collection include poker hand (a collection of cards), a mail folder (a collection of letters) or a telephone directory (a mapping of names to phone numbers).

Photo by Victoire Joncheray on Unsplash

What is a Collections Framework?

A collections framework is a unified architecture for representing and manipulating collections. — Oracle Java Documentation

All collections frameworks contain:

  1. Interfaces: Abstract Data Types (ADT) that represent collections. Interfaces allow collections to be manipulated independently of the details of their representation. These interfaces generally form a hierarchy.
  2. Implementations: concrete implementations of the collection interfaces. They are reusable data structures.
  3. Algorithms: methods that perform useful computations, such as sorting and searching, on objects that implement collection interfaces. The algorithms are said to be polymorphic: that is, the same method can be used on many different implementations of the appropriate collection interface. In essence, algorithms are reusable functionality.

Basic operations with the collection framework

  1. Add objects to the collection.
  2. Remove objects from the collection.
  3. Find out if an object is in the collection.
  4. Retrieve an object (without removing it).
  5. Iterate through the collection.

Hierarchy of Collections Framework

java.util package contains all of the classes and interfaces for the collections framework.

Figure 1: Hierarchy of Collections Interface. Source
Figure 2: Hierarchy of Map Interface. Source

NOTE: Map is not a true collection.

All the core collection interfaces are generic.

For example, this is the declaration of the Collection interface:

public interface Collection<E>

When you declare a Collection instance you should specify the type of object contained in the collection.

Specifying the type allows the compiler to verify (at compile time) that the type of object you put into the collection is correct, thus reducing errors at runtime.

Core Collection Interfaces

Now, let’s see briefly about all core collection interfaces.

1. Collection

Root of the collection hierarchy. A collection represents a group of objects known as its elements. Java does not allow to implement the Collection interface directly but we can implement its sub interfaces such as Set, List and Queue.

2. Set

A collection that can’t contain duplicate elements. Unique things. No particular order.

3. List

An ordered collection (ordered by index position) but it can contain duplicate elements. List of things. We can access elements of List using its index positions.

4. Queue

A collection used to hold multiple elements prior to processing. Most of the time (not always) elements are ordered in a FIFO (first-in, first-out) manner.

5. Deque

A collection used to hold multiple elements prior to processing. Deques can be used both as FIFO (first-in, first-out) and LIFO (last-in, first-out).

6. Map

An object that maps keys to values. A Map can not contain duplicate keys. Each key can map to at most one value. Things with unique ID. No particular order.

7. NavigableSet

8. NavigableMap

9. SortedSet

SortedSet extends the Set interface. It maintains its elements in ascending order. Sorted sets are used for naturally ordered sets, for example, word lists and membership roles.

10. SortedMap

A Map that maintains its mappings in ascending key order. This is the Map analog of SortedSet. Sorted maps are used for naturally ordered collections of key/value pairs, such as dictionaries and telephone directories.

Conclusion

I hope this article provided you some basic information regarding Collections framework.

Thank you for reading!

References

--

--

Thanoshan MV
Thanoshan MV

Written by Thanoshan MV

Hi! I'm a Software Engineer with a passion for research in AI, software engineering, and open-source development. Web - https://thanoshanmv.github.io/

No responses yet