Collection Framework

The Java Collections framework standardizes how your programs handle a group of objects. Before Java2, Java provided ad hoc classes such as Vector, Stack, Dictionary, Hashtable and Properties to store and manipulate groups.

Although these classes were quite useful, they lacked a central unifying theme. The way we use Vector was different from how we use properties class; also, the previous ad hoc approach was not designed to be easily extensible or adaptable. Collections are an answer to these (and other) problems.

The Collection framework was designed, keeping into consideration the following objectives:

  • The Framework has to be high-performance. The entire collections (dynamic arrays, linked lists, trees, and hash tables) are highly efficient.
  • The Framework has to allow different types of collections to work in a similar manner and with a high interoperability degree.
  • Extending and adapting to a collection had been easy.
To achieve the above goals, the collection framework design the following features:
  • Entire Collection framework is designed around a set of standard interfaces. The several standard implementations of these interfaces are provided. We may also implement our collection.
  • Another item created by the collections framework is the Iterator interface. An Iterator gives you a general-purpose, standardized way of accessing the element within a collection, one at a time.
  • Thus, an Iterator provides a means of enumerating the contents of a collection. Because each group implement Iterator, the elements if any collection class can be accessed through the Iterator’s methods, this, with only small changes, the code that cycle through a set can also be used to cycle through a list, for example.
  • In addition to collections, the framework defines several map interfaces and classes. Maps store key/value pairs. Although maps are not “collection” in the proper use of the term, they are fully integrated with collections.
  • In the language of the collection framework, we can obtain a collection view of the map. Such an idea contains the elements form the map stored in a collection. Thus, we can process the contents of a plan as a collection, if you choose.
  • The collection mechanism was retrofitted to some of the original classes defined by java.util package so that they too could be integrated into the new system.
  • It is essential to understand that although collections altered many of the original utility classes’ architecture, it did not cause the deprecation of any. Groups simply provide a better way of doing several things.
The Collection Interfaces:

The collections framework defines several interfaces. The concrete classes provide different implementations of the standard interfaces. The interfaces that underpin collections are:


Enable you to work with a group of objects; it is at the top of the collections hierarchy.


Extends collection to handle sequences (lists of objects).


Extends collection to handle sets, which must contain unique elements.


Extends collection to handle sorted sets.

In addition to the collection interfaces, collections also use the Comparator, Iterator and RandomAccess interfaces. The comparator defines how two objects are compared; Iterator and ListIterator enumerate the items within a collection.

By implementing RandomAccess, a list indicates that it supports efficient, random access to its elements. To provide the greatest flexibility in their use, the collection interfaces allow some methods to be optional.

The optional methods enable you to modify the contents of a collection. Collections that support these methods are called elastic. Collections that do not allow their contents to be changed are called unmodifiable.

If an attempt is made to use one of these methods on an unmodifiable collection an UnsupportedOperationExceprin is thrown. All the built-in collections are modifiable.

The Iterator Interface

Iterator interface defines the following methods:

Boolean hasNext()

returns true if the iteration has more elements.
Object next()

returns the next element in the iteration.
void remove()

Removes from the underlying collection the last element returned by the iterator (may not allow doing so in some circumstances as in EJB’s). It is an optional operation, so it may throw UnsupportedOperationException if iterator does not support this operation.
Iterators differ from enumerations in two ways:
  • Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantic.
  • Method names have been improved.
The ListIterator Interface :

This is a subinterface of Iterator.

public interface ListIterator extends Iterator

It is an iterator for lists that allows the programmer to traverse4 the list in either direction, modify the list during iteration, and obtain the iteator’s current position in the list.

Method of Collection Interface:

Following are the methods declared in the Collection interface:

  • void add(Object obj)

    Inserts the specified element into the list (optional operation).

  • boolean hasNext()

    Returns true if this list iterator has more elements when traversing the listin the forward direction.

  • boolean hasPrevious()

    Returns true if this list iterator has more elements when traversing the listin the reverse direction.

  • Object next()

    Return the next element in the list.

  • int nextIndex()

    Returns the index if the element that would be returned by a subsequent call to next.

  • Object previous()

    Return the previous element in the list.

  • int previousIndex()

    Returns the index if the element that would be returned by a subsequent call to previous.

  • void remove()

    Remove from the list the last element that was returned by next or previous (optional operation).

  • void set(Object obj)

    Replaces the last element returned by next or previous with the specified element (optional operation).

Copyright 1999- Ducat Creative, All rights reserved.