Legacy Classes
Legacy Classes
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
6. Vector
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()
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.
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
1. import java.util.*;
3. {
5. {
7. vec.add("Emma");
8. vec.add("Adele");
9. vec.add("Aria");
10. vec.add("Aidan");
11. vec.add("Adriana");
12. vec.add("Ally");
14. while(data.hasMoreElements())
15. {
16. System.out.println(data.nextElement());
17. }
18. }
19. }
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()
2) Hashtable(int size)
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.
HashtableExample.java
1. import java.util.*;
2. class HashtableExample
3. {
5. {
13. while(iterate.hasNext())
14. {
17. }
18. }
19. }
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:
Properties()
2) Properties(Properties propdefault)
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
1. import java.util.*;
3. {
4. public static void main(String[] args)
5. {
7. prop_data.put("India", "Movies.");
wers.");
14. {
));
16. }
17. }
18. }
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()
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
1. import java.util.*;
2. class StackExample {
5. stack.push("Emma");
6. stack.push("Adele");
7. stack.push("Aria");
8. stack.push("Ally");
9. stack.push("Paul");
11. while(enum1.hasMoreElements())
13. stack.pop();
14. stack.pop();
15. stack.pop();
18. while(enum2.hasMoreElements())
20. }
21. }
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.
1. The put(K key, V value) method is used to add a key-value pair to the
dictionary.
3. The get(Object key) method is used to get the value mapped with the
argumented key in the dictionary.
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.
DictionaryExample.java
1. import java.util.*;
3. {
5. {
8.
15.
18. {
ement());
20. }
21.
25.
+ "\n");
28.
31. {
33. }
34.
));
38.
));
40.
41. }
42. }