Java - Java Collection Framework

What Is a Collection?

A collection, also known as a container, is an object that contains a group of objects.

Each object in a collection is called an element of the collection.

Architecture of the Collection Framework

The Collections Framework consists of three main components:

  • Interfaces
  • Implementation Classes
  • Algorithm Classes

Interface

An interface represents a specific type of collection in the framework.

There is one interface defined for every type of collection in the framework.

  • List interface represents a list.
  • Set interface represents a set.
  • Map interface represents a map.

Implementation

The Collections Framework provides implementations of collection interfaces.

The following code shows how to use the implementation class ArrayList to create a list.

Create an instance of the ArrayList class storing the reference in a variable of the List interface

List<String> names = new ArrayList<>();

Algorithm

To perform different actions on a collection, such as

  • searching through a collection,
  • converting a collection of one type to another type,
  • copying elements from one collection to another,
  • sorting elements of a collection in a specific order, etc.

Use algorithm classes on your collections.