Introduction

Java versions earlier to Java2 did not contain any collections framework. They only contained some classes and an interface, which were used to store objects. These are called as Legacy classes and Legacy Interface. The Legacy classes are synchronized as opposed to the classes in the collection framework. The Legacy classes are Dictionary, Hashtable, Properties, Stack, and Vector. The Legacy interface is the Enumeration interface.

Dictionary

The Dictionary is an abstract class. It maps keys to values and operates similar to a Map. Every key and value is an object in this class. Each key is mapped to utmost one value. This mapping enables us to retrieve the value if the key is known. The Dictionary class is declared obsolete in Java2 as it is superseded by the Map class. It contains only the constructor Dictionary()

Methods of Dictionary class

Hashtable

Hashtable stores key value pairs in a hash table. We must specify the key and the value to be mapped to that key. The key is hashed and the hash code is used as the index of the position at which the value is stored. The object of the Hashtable class must override the hashCode() and equals() method of the Object class. The hashCode() method is used to convert the key to its equivalent hash code, and the equals() method is used to compare two objects. The hash table contains 4 constructors.

The default load factor is 0.75 and it can vary from 0.0 to 1.0. In addition to the methods of the Map interface class defines the methods.

In addition to the methods of the Map interface, the Hastable class defines some methods :

The following example illustrates the usage of Hashtable. It uses the example of marks scored by the student.

import java.util.*;
class HashtableEg {
    public static void main(String args[]) {
        Hashtable h = new Hastable();
        h.put("Ashish", new Integer(70));
        h.put("Sonali", new Integer(65));
        h.put("Soniya", new Integer(58));
        h.put("Akash", new Integer(80));
        h.put("Anuj", new Integer(78));
        Enumeration e = h.elements();
        while (e.hasMoreElements()) {
            System.out.println(e.nextElement());
        }
    }
}

Properties

The Properties class extends the Hashtable class. The properties class represents a persistent set of properties. Each key and its value in the property list is a string. It can also contain another property list as its “defaults”. The default property list will be searched if the key is not present in the current property list. Properties class is used by many other Java classes.

The properties class contains its own methods in addition to the methods it inherits from Hashtable.

Vector

The Vector class implements a growable array of objects. Its components can be accessed using an integer index. Each vector maintains a capacity and capacity increment. The capacity is always greater than or equal to the vector size.

The following constructor of vector class as,

Below program illustrate the usage of Vector class.

import java.util.*;
class VectorEg {
    public static void main(String args[]) {
        Vector v = new Vector();
        v.add("First");
        int i = 1;
        v.add(new Integer(i));
        double d = 1.1;
        v.add(new Double(d));
        v.add("Second");
        i = 2;
        v.add(new Integer(i));
        d = 2.2;
        v.add(new Double(d));
        Enumeration e = v.elements();
        System.out.println("The elements of the vector are as follows:\n");
        while (e.hasMoreElements()) {
            System.out.println(e.nextElement());
        }
        System.out.println("\n");
        System.out.println("The Capacity of the vector is:" + v.capacity());
        System.out.println("The Size of the vector is:" + v.size());
        System.out.println("The Second element  of the vector is:" + v.elementAt(1));
        System.out.println("The First element  of the vector is:" + v.firstElement());
        System.out.println("The Last element  of the vector is:" + v.lastElement());
        v.removeElementAt(2);
        e = v.elements();
        System.out.println("\n The Vector after removing one of its elements is as follows:\n");
        while (e.hasMoreElements()) {
            System.out.println(e.nextElement());
        }
        System.out.println("\n");
    }
}

Stack

The stack class represents a last-in-first-out stack of objects and extends the Vector class. It contains a single constructor, namely Stack() that creates an empty stack. It contains five methods that allow a Vector to be treated as a stack.

Enumeration Interface

The Enumeration interface is the only legacy interface. It defines methods, which help us to enumerate the elements in a collection of objects. This interface has been suspended by Iterator. It contains only 2 methods as shown here:

Summary

The collection interface is at the top of the hierarchy. It enables us to work with groups of objects. The List interface extends the Collection interface and handles sequences. The legacy classes are Dictionary, Hashtable, Properties, Stack, and vector. The legacy interface is the Enumeration interface.