Legacy Classes – Java Collections
Early version of java did not include the Collections framework. It only defined several classes and interfaces that provide methods for storing objects. When Collections framework were added in J2SE 1.2, the original classes were reengineered to support the collection interface. These classes are also known as Legacy classes. All legacy classes and interface were redesign by JDK 5 to support Generics. In general, the legacy classes are supported because there is still some code that uses them.
The following are the legacy classes defined by java.util package
- Dictionary
- HashTable
- Properties
- Stack
- Vector
There is only one legacy interface called Enumeration
NOTE: All the legacy classes are synchronized
Enumeration interface
-
- Enumeration interface defines method to enumerate(obtain one at a time) through collection of objects.
- This interface is superseded(replaced) by Iterator interface.
- However, some legacy classes such as Vector and Properties defines several method in which Enumeration interface is used.
- It specifies the following two methods
Vector class
- Vector is similar to ArrayList which represents a dynamic array.
- There are two differences between Vector and ArrayList. First, Vector is synchronized while ArrayList is not, and Second, it contains many legacy methods that are not part of the Collections Framework.
- With the release of JDK 5, Vector also implements Iterable. This means that Vector is fully compatible with collections, and a Vector can have its contents iterated by the for-each loop.
- Vector class has following four constructor
- Â
Vector defines several legacy methods. Lets see some important legacy methods defined by Vector class.
Method | Description |
---|---|
void addElement(E element) | adds element to the Vector |
E elementAt(int index) | returns the element at specified index |
Enumeration elements() | returns an enumeration of element in vector |
E firstElement() | returns first element in the Vector |
E lastElement() | returns last element in the Vector |
void removeAllElements() | removes all elements of the Vector |
Example of Vector
10 20 30 40 50 60
Hashtable class
- Like HashMap, Hashtable also stores key/value pair. However neither keys nor values can be null.
- There is one more difference between HashMap and Hashtable that is Hashtable is synchronized while HashMap is not.
- Hashtable has following four constructor
- Â
Example of Hashtable
a 100 b 200 c 300 d 400
Difference between HashMap and Hashtable
Hashtable | HashMap |
---|---|
Hashtable class is synchronized. | HashMap is not synchronized. |
Because of Thread-safe, Hashtable is slower than HashMap | HashMap works faster. |
Neither key nor values can be null | Both key and values can be null |
Order of table remain constant over time. | does not guarantee that order of map will remain constant over time. |
Properties class
- Properties class extends Hashtable class.
- It is used to maintain list of value in which both key and value are String
- Properties class define two constructor
-
- Â
- One advantage of Properties over Hashtable is that we can specify a default property that will be useful when no value is associated with a certain key.
Note: In both cases, the property list is empty
- In Properties class, you can specify a default property that will be returned if no value is associated with a certain key.
Example of Properties class
Java was created by James Ghosling C++ was created by Bjarne Stroustrup C was created by Dennis Ritchie C# was created by Microsoft Inc
Stack class
- Stack class extends Vector.
- It follows last-in, first-out principle for the stack elements.
- It defines only one default constructor
Stack() //This creates an empty stack
- If you want to put an object on the top of the stack, call push() method. If you want to remove and return the top element, call pop() method. An EmptyStackException is thrown if you call pop() method when the invoking stack is empty.
You can use peek() method to return, but not remove, the top object. The empty() method returns true if nothing is on the stack. The search() method determines whether an object exists on the stack and returns the number of pops that are required to bring it to the top of the stack.
Example of Stack
11 22 33 44 55 After popping out two elements 11 22 33
Dictionary class
- Dictionary is an abstract class.
- It represents a key/value pair and operates much like Map.
- Although it is not currently deprecated, Dictionary is classified as obsolete, because it is fully superseded by Map class.