Javatpoint Logo
Javatpoint Logo

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:

  1. HashTable
  2. Stack
  3. Dictionary
  4. Properties
  5. Vector
Legacy Class in Java

Vector Class

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:

1) Vector()

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.

VectorExample.java

Output:

Legacy Class in Java

Hashtable Class

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.

1) Hashtable()

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.

HashtableExample.java

Output:

Legacy Class in Java

Properties Class

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:

1) Properties()

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.

PropertiesExample.java

Output:

Legacy Class in Java

Stack Class

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().

1) Stack()

It is used to create a stack without having any elements.

There are the following methods can be used with Stack class:

  1. The push() method is used to add an object to the stack. It adds an element at the top of the stack.
  2. The pop() method is used to get or remove the top element of the stack.
  3. The peek() method is similar to the pop() method, but it can't remove the stack's top element using it.
  4. The empty() method is used to check the emptiness of the stack. It returns true when the stack has no elements in it.
  5. The search() method is used to ensure whether the specified object exists on the stack or not.

StackExample.java

Output:

Legacy Class in Java

Dictionary 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:

  1. The put(K key, V value) method is used to add a key-value pair to the dictionary.
  2. The elements() method is used to get the value representation in the dictionary.
  3. The get(Object key) method is used to get the value mapped with the argumented key in the dictionary.
  4. The isEmpty() method is used to check whether the dictionary is empty or not.
  5. The keys() method is used to get the key representation in the dictionary.
  6. The remove(Object key) method removes the data from the dictionary.
  7. The size() method is used to get the size of the dictionary.

DictionaryExample.java

Output:

Legacy Class in Java

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.






Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA