Java Collections Framework

A collection in Java represents a group of objects that can be stored, retrieved, and manipulated as its elements. It is just like a container that basically provides grouping of multiple elements into a single unit.

The collections framework is a unified architecture for representing and manipulating collections. It is available in the java.util package.

The parent in this framework is the Collection interface that extends java.lang.Iterable which adds support for iterating over its elements. 

Collections can be used in day-to-day life such as storing mobile numbers, employee names database etc. They basically provide grouping of multiple elements into a single unit.

Some collections are ordered and others are unordered. Some collections allow duplicate elements while others don’t.

Prerequisite: One must have the concept of “Generics” to understand Collections Framework.

 

The Collections Framework mainly consists of the following three components:

1. Core InterfacesA set of Interfaces which allows the collections to be manipulated independently of their implementation. These interfaces define the common functionalities presented by the collections and eases data exchange between collections.

2. Concrete ClassesThese are the Implementations for most of the interfaces, providing data structures that a program can use readily.

3. Utility MethodsA set of standard Algorithms that can be used to perform various operations on collections, such as searching and sorting, or building customized collections.

In addition, the framework also provides several abstract implementations, which are very useful for us to create new and different implementations for handling collections of data.

 

Advantages of Collection Framework:

  • Consistent and reusable API – The API has a basic set of interfaces like Collection, Set, List, Queue or Map. All classes (ArrayList, LinkedList, Vector etc.) that implement these interfaces have some common set of utility methods for solving common problems related to a group of objects in a consistent manner.
  • Reduced development timeA consistent framework always reduces the development time as it helps the programmer in writing application program quickly without worrying about the design issues of Collection.
  • Increased performance and quality – Increases performance by providing high-performance implementations of useful data structures and algorithms.
  • Neat and clean code – Collection API has been written with very high-standard coding practices and documented very well. It makes the code look neat and clean.
 
The three components of Java Collections Framework are described below.

1. Core Interfaces

The core interfaces that define common functionalities and allow collections to be manipulated are independent of their implementations.

The core Interfaces of the collections framework are:

  • Collection
  • Set
  • List
  • Queue
  • Map

As we can see in the following figure, the Core Collection interfaces form a hierarchy.

Figure 1: Hierarchy of Core interfaces of Collection

 

A Set is actually a particular type of Collection; a SortedSet is a particular type of Set, and so on. It is to be noted that the hierarchy consists of two distinct trees, as because Map is not a true Collection.

All the Core collection interfaces are generic. For example, the actual signature of java.util.Collection interface is as follows:

public interface Collection<E> extends Iterable<E>

//The syntax tells us that this interface is generic.

 

Here, super-interface java.lang.Iterable has the following signature :

public interface Iterable<T>

The interface Iterable has got only one method named iterator() for iterating over collection elements.

Iterator<T> iterator(): Returns an iterator over a set of elements of type T.

NOTE: The special interface “Iterator” is described at the bottom of this tutorial.

 

The following describes the core interfaces other than the Collection interface:

  • Set — It is a collection that does not allow duplicate elements.
  • List — It is an ordered collection that allows duplicate elements. We can easily access and manipulate its elements using their index position.
  • Queue — It is a collection that contains multiple elements prior to processing. Queues generally process elements in a FIFO (first-in, first-out) manner.
  • Map — It is an object that maps keys to values similar to a Hashtable. A Map does not allow duplicate keys. Every key in this collection can map to at most one value.  

The last two core collection interfaces are simply the sorted versions of Set and Map:

  • SortedSet — It is a Set that preserves its elements in ascending order.
  • SortedMap — It is a Map that preserves its mappings in ascending key order.

 

2. Concrete Classes

The concrete classes are specific implementations of the core interfaces, providing data structures that the Java programs can use. As we can see in the following figure, core interfaces and concrete classes form a hierarchy. Remember that all of these are part of the java.util package.


Figure 2: Collections framework interface and class hierarchy

 

The standard concrete classes are summarized in the following table.

TABLE 1. Classes of collections framework

Sl.No.Class and Description
1

AbstractCollection

Implements most of the Collection interface.

2

AbstractList

Extends AbstractCollection and implements most of the List interface.

3

AbstractSequentialList

Extends AbstractList for use by a collection that uses sequential rather than random access of its elements.

4

LinkedList

Implements a linked list by extending AbstractSequentialList.

5

ArrayList

Implements a dynamic array by extending AbstractList.

6

AbstractSet

Extends AbstractCollection and implements most of the Set interface.

7

HashSet

Extends AbstractSet for use with a hash table.

8

LinkedHashSet

Extends HashSet to allow insertion-order iterations.

9

TreeSet

Implements a set stored in a tree. Extends AbstractSet.

10

AbstractMap

Implements most of the Map interface.

11

HashMap

Extends AbstractMap to use a hash table.

12

TreeMap

Extends AbstractMap to use a tree.

13

Hashtable

Hashtable was part of the original java.util and is a concrete implementation of a Dictionary.

14

Vector

This implements a dynamic array. It is similar to ArrayList, but with some differences.

15

Stack

Stack is a subclass of Vector that implements a standard last-in, first-out stack.

The abstract classes namely AbstractCollection, AbstractSet, AbstractList, AbstractSequentialList and AbstractMap (in bold font) provide skeletal implementations of the core collection interfaces, to minimize the coding effort required to implement them.

 

3. Utility Methods

The Collection interface is used to represent any group of objects, or elements. It has got several methods for high-performance implementations of useful data structures and algorithms. The following table lists some important methods of this interface.

TABLE 2. Methods of Collection interface

Method

Description

boolean add (E o)

Ensures that this collection contains the specified element.

boolean addAll (Collection<? extends E> c)

Adds all of the elements in the specified collection to this collection.

void clear()

Removes all of the elements from this collection.

boolean contains (Object o)

Returns true if this collection contains the specified element.

boolean containsAll (Collection<?> c)

Returns true if this collection contains all of the elements in the specified collection.

boolean equals (Object o)

Compares the specified object with this collection for equality.

boolean isEmpty()

Returns true if this collection contains no elements.

Iterator iterator()

Returns an iterator over the elements in this collection.

Object[ ]  toArray()

Converts collection into array.

boolean remove (Object o)

Removes a single instance of the specified element from this collection, if it is present.

boolean removeAll (Collection<?> c)

Removes all this collection’s elements that are also contained in the specified collection.

int size()

Returns the number of elements in this collection.

 

 

Iterator Interface

Here, E is the type of elements returned by this iterator. The iterator() method of the Collection interface returns an Iterator. An Iterator is similar to the Enumeration interface; Iterators differ from enumerations in two ways:

  1. Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.
  2. Method names have been improved.

Table 3 below lists the the methods of this interface.

TABLE 3. Method list of interface Iterator

Method

Description

boolean hasNext()

Returns true if the iteration has more elements.

E next()

Returns the next element in the iteration.

void remove()

Removes from the underlying collection the last element returned by the iterator.

The remove() method is optionally supported by the underlying collection. When called and supported, the element returned by the last next() call is removed.

In the following sections, I will describe about the important interfaces and classes in the Collections Framework.