Java Collections Framework


A collection — sometimes called a container — is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data. Typically, they represent data items that form a natural group, such as a poker hand (a collection of cards), a mail folder (a collection of letters), or a telephone directory (a mapping of names to phone numbers).

A collections framework is a unified architecture for representing and manipulating collections. All collections frameworks contain the following:

  • Interfaces: These are abstract data types that represent collections. Interfaces allow collections to be manipulated independently of the details of their representation. In object-oriented languages, interfaces generally form a hierarchy.
  • Implementations: These are the concrete implementations of the collection interfaces. In essence, they are reusable data structures.
  • Algorithms: These are the methods that perform useful computations, such as searching and sorting, 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.

The core collection interfaces encapsulate different types of collections, which are shown in the figure below. These interfaces allow collections to be manipulated independently of the details of their representation. Core collection interfaces are the foundation of the Java Collections Framework. As you can see in the following figure, the core collection interfaces form a hierarchy.


A Set is a special kind of Collection, a SortedSet is a special kind of Set, and so forth. Note that all the core collection interfaces are generic. For example, this is the declaration of the Collection interface.

The hierarchy consists of two distinct trees — a Map is not a true Collection. Above you can see what a Map collection consists:


public interface Collection<E>...

The <E> syntax tells you that the interface is generic. When you declare a Collection
instance you can and 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.The following list describes the core collection interfaces:

  • Collection — the root of the collection hierarchy. A collection represents a group of objects known as its elements. The Collection interface is the least common denominator that all collections implement and is used to pass collections around and to manipulate them when maximum generality is desired.
  • Set — 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.
  • SortedSet — a Set that maintains its elements in ascending order. Several additional operations are provided to take advantage of the ordering. Sorted sets are used for naturally ordered sets, such as word lists and membership rolls.
  • List — an ordered collection (sometimes called a sequence). Lists 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 integer index (position).
  • Queue — a collection used to hold multiple elements prior to processing. Besides basic Collection operations, a Queue provides additional insertion, extraction, and inspection operations.Queues typically, but do not necessarily, order elements in a FIFO (first-in, first-out) manner.
  • Deque — a collection used to hold multiple elements prior to processing. Besides basic Collection operations, a Deque provides additional insertion, extraction, and inspection operations.Deques 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.

The Map interface is separate for the Collection interface:

  • Map an object that maps keys to values. A Map cannot contain duplicate keys; each key can map to at most one value.
  • 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.

Benefits of the Java Collections Framework

The Java Collections Framework provides the following benefits:

    • Reduces programming effort
    • Increases program speed and quality
    • Allows interoperability among unrelated APIs
    • Reduces effort to learn and to use new APIs
    • Reduces effort to design new APIs
    • Fosters software reuse

Implementations are the data objects used to store collections, which implement the interfaces.

There are different kinds of implementations:

  • General-purpose implementations are the most commonly used implementations, designed for everyday use.general_purpose_implementatios
  • Special-purpose implementations are designed for use in special situations and display nonstandard performance characteristics, usage restrictions, or behavior.
  • Concurrent implementations are designed to support high concurrency, typically at the expense of single-threaded performance. These implementations are part of the java.util.concurrent package.
  • Wrapper implementations are used in combination with other types of implementations, often the general-purpose ones, to provide added or restricted functionality.
  • Convenience implementations are mini-implementations, typically made available via static factory methods, that provide convenient, efficient alternatives to general-purpose implementations for special collections (for example, singleton sets).
  • Abstract implementations are skeletal implementations that facilitate the construction of custom implementations. An advanced topic, it’s not particularly difficult, but relatively few people will need to do it.

The polymorphic algorithms described here are pieces of reusable functionality provided by the Java platform. All of them come from the Collections class, and all take the form of static methods whose first argument is the collection on which the operation is to be performed. The great majority of the algorithms provided by the Java platform operate on List instances, but a few of them operate on arbitrary Collection instances.

  1. Sorting

  2. Shuffling

  3. Routine Data Manipulation

  4. Searching

  5. Composition

  6. Finding Extreme Values

Collection Advantages and Disadvantages

  • it provides useful data structures and algorithms that reduces programming effort due to which we need not to write them ourselves
  • can hold different types of objects
  • they are resizable and can grow
  • reduces the effort required to design and implement APIs by eliminating the need to produce ad hoc collections APIs


    • must cast to correct type
    • cannot do compile-time type checking

By implementing a common interface in the Collections framework, you make it easy for other programmers to access your collection, you reduce the time it takes for others to learn your class, and you make your class more useful. The Java collections framework gives the programmer access to prepackaged data structures as well as to algorithms for manipulating them.