Legacy Class in Java
In the past decade, the Collection framework didn't include in Java. In the early version of Java, we have several classes and interfaces which allow us to store objects. After adding the Collection framework in JSE 1.2, for supporting the collections framework, these classes were re-engineered. So, classes and interfaces that formed the collections framework in the older version of Java are known as Legacy classes. For supporting generic in JDK5, these classes were re-engineered.
All the legacy classes are synchronized. The java.util package defines the following legacy classes:
Vector is a special type of ArrayList that defines a dynamic array. ArrayList is not synchronized while vector is synchronized. The vector class has several legacy methods that are not present in the collection framework. Vector implements Iterable after the release of JDK 5 that defines the vector is fully compatible with collections, and vector elements can be iterated by the for-each loop.
Vector class provides the following four constructors:
It is used when we want to create a default vector having the initial size of 10.
2) Vector(int size)
It is used to create a vector of specified capacity. It accepts size as a parameter to specify the initial capacity.
3) Vector(int size, int incr)
It is used to create a vector of specified capacity. It accepts two parameters size and increment parameters to specify the initial capacity and the number of elements to allocate each time when a vector is resized for the addition of objects.
4) Vector(Collection c)
It is used to create a vector with the same elements which are present in the collection. It accepts the collection as a parameter.
The Hashtable class is similar to HashMap. It also contains the data into key/value pairs. It doesn't allow to enter any null key and value because it is synchronized. Just like Vector, Hashtable also has the following four constructors.
It is used when we need to create a default HashTable having size 11.
2) Hashtable(int size)
It is used to create a HashTable of the specified size. It accepts size as a parameter to specify the initial size of it.
3) Hashtable(int size, float fillratio)
It creates the Hashtable of the specified size and fillratio. It accepts two parameters, size (of type int) and fillratio (of type float). The fillratio must be between 0.0 and 1.0. The fillratio parameter determines how full the hash table can be before it is resized upward. It means when we enter more elements than its capacity or size than the Hashtable is expended by multiplying its size with the fullratio.
4) Hashtable(Map< ? extends K, ? extends V> m)
It is used to create a Hashtable. The Hashtable is initialized with the elements present in m. The capacity of the Hashtable is the twice the number elements present in m.
Properties class extends Hashtable class to maintain the list of values. The list has both the key and the value of type string. The Property class has the following two constructors:
It is used to create a Properties object without having default values.
2) Properties(Properties propdefault)
It is used to create the Properties object using the specified parameter of properties type for its default value.
The main difference between the Hashtable and Properties class is that in Hashtable, we cannot set a default value so that we can use it when no value is associated with a certain key. But in the Properties class, we can set the default value.
Stack class extends Vector class, which follows the LIFO(LAST IN FIRST OUT) principal for its elements. The stack implementation has only one default constructor, i.e., Stack().
It is used to create a stack without having any elements.
There are the following methods can be used with Stack class:
The Dictionary class operates much like Map and represents the key/value storage repository. The Dictionary class is an abstract class that stores the data into the key/value pair. We can define the dictionary as a list of key/value pairs.
The dictionary class provides the following methods:
All the classes that we have discussed above are known as legacy classes. For supporting generic in JDK5, the above-discussed classes were re-engineered.