The Java Collections components

Many of the data structures we studied are already coded for you in the Java libraries, within the package, java.util. Here is a brief summary.

Stacks and Vectors

Two classes belong to the earliest version of java.util:

The ``Collections Framework''

Within java.util is a a family of data structures that share standard operations and properties; they are called the ``collections framework.'' The framework is ``defined'' by several Java interfaces that state some standard operations that classes in the framework must implement.

Here are the two most important interfaces:

Here are two interfaces that add more operations to Collection:

Classes that implement interface List

There are two important classes that implement interface List, that is, are numbered sequences:

You should use these two classes to build other data structures that must be ``smart'' arrays or ``smart'' linked lists. For example, you might build a class Queue like this:

import java.util.*;
public class Queue
{ private LinkedList my_queue;

  public Queue()
  { my_queue = new LinkedList(); }

  public enqueue(Object ob)
  { my_queue.add(ob); }  // adds ob to the end of the linked list

  public Object dequeue()
  { Object answer = null;
    if ( !my_queue.isEmpty() )
       { answer = my_queue.remove(0); } // remove the front object in the list
    return answer;

Classes that implement interface Map

These classes store ``Records'' --- objects paired with their keys:

Classes that implement interface Set

Interface Set is supposed to describe data structures that implement sets, having operations for set membership, union, and intersection. The Java language does not do well at providing set data structures, so instead we are asked to choose between a tree-simulation of a set and a hash-table simulation of a set. Neither solution is ideal. Indeed, both TreeSets and HashSets are really just TreeMaps and HashMaps that manufacture their own keys for the objects that are inserted.


One standard sticky problem with data structures is printing the structure's contents in a simple way --- for example, we might copy the objects within a binary tree or a hash table into an array and return the array for printing.

An iterator is an ordered ``array'' of the contents of a data structure. There is a Java interface, interface Iterator. An iterator has at least these two methods:

To understand these operations, let's compare them to an array.

Say that we copied the objects held in a tree into an array named iter. Then we write this loop to print the contents:

Object[] iter =  ... copy contents of tree into array ...

for ( int i = 0;  i != iter.length;  i = i + 1 )
    { Object next_object = iter[i];
      System.out.println( next_object.toString() );
        // remember that  toString  is a Java method that tries to convert
	// an object into a string for printing.  It often works!
You do the same work with an iterator: Say that you built a data structure, my_data_structure, with one of the Java Collections classes listed above. Next, you added some objects into the structure, and now you want to print the contents:
Iterator iter = my_data_structure.iterator();  // copies the objects in
                    // my data structure into an ``array'' named  iter
while ( iter.hasNext() )  // are there more objects to look at ?
      { Object next_object =;   // get the next object
        System.out.println( next_object.toString() );
The iterator structure hides the details about whether an array or a linked list or whatever is the best structure for returning the contents of a data structure for printing.


Now that you understand and know how to program linked lists, ordered trees, and hash tables, you can intelligently use the classes in the Java Collections package and save time when you are next asked to build a ``smart'' data structure.

You can read the local documentation for java.util at
Or, visit Sun's web site,, for the latest writeup.