What are the Legacy Classes in Java?
Before Java 1.2, there were multiple classes and interfaces to hold the objects. There was no Collection Framework before this version. In that circumstance, legacy classes and interfaces are employed to store things.
All legacy classes have been synchronized. The following legacy classes are defined in java.util package:
- HashTable
- Stack
- Dictionary
- Properties
- Vector
Enumeration is the only remaining legacy interface.
Vector Class:
Vector is an ArrayList subclass that defines a dynamic array. ArrayList is not synchronized, whereas vector is. There are some historical methods in the vector class that is not included in the collection framework. After the introduction of JDK 5, Vector implements Iterable, which defines that the vector is fully compatible with collections and that vector items can be iterated using the for-each loop.
Vector class constructors:
- Vector() This generates a default vector with a size of 10 by default.
- Vector(int size) This produces a vector with an initial capacity determined by size.
- Vector(int size, int inc) This generates a vector with the initial capacity defined by size and the increment specified by inc. The increment determines how many elements to allocate each time a vector is resized for object addition.
- Vector(Collection c) This produces a vector containing elements from collection c.
Vector Methods:
Method | Description |
E elementAt(int index) | returns the element at the specified index |
E firstElement() | returns the first element in the Vector |
Enumeration elements() | returns an enumeration of the element in the vector |
E lastElement() | Returns the last element in the Vector |
void removeAllElements() | removes all the elements of the Vector |
Example:
import java.util.*;
public class Coders
{
public static void main(String[] args)
{
Vector<Integer> ve = new Vector<Integer>();
ve.add(101);
ve.add(201);
ve.add(103);
ve.add(42);
ve.add(501);
ve.add(116);
Enumeration en = ve.elements();
while(en.hasMoreElements())
{
System.out.println(en.nextElement());
}
}
}
Output:
101
201
103
42
501
116
Hashtable Class:
Hashtable is a part of Java.util package and it is a concrete class that extends the dictionary class. Hashtable is synchronized. From Java 1.2 framework onwards, the hash table class implements the map interface, and it is part of the collection framework.
Hashtable class constructors:
- Hashtable() This is the default constructor with the default size of 11.
- Hashtable(int size) This produces a hash table with the initial size defined by size.
- Hashtable(int size, float fillratio) This produces a hash table with the starting size defined by size and the fill ratio specified by fillRatio. This ratio, which must be between 0.0 and 1.0, governs how full the hash table can be before resizing upward. The hash table is enlarged when the number of elements exceeds the capacity of the hash table multiplied by its fill ratio. If no fill ratio is specified, 0.75 is utilized.
- Hashtable(Map< ? extends K, ? extends V> m) This generates a hash table with the elements in m as its initialization. The hash table’s capacity is set to double the number of elements in m. The load factor of 0.75 is utilized by default.
Example:
import java.util.*;
public class Coders
{
public static void main(String[] args)
{
Hashtable<Integer,String> fruits = new Hashtable<Integer, String>();
fruits.put(new Integer(101), "Orange");
fruits.put(new Integer(201), "Kiwi");
fruits.put(new Integer(121), "Cherry");
fruits.put(new Integer(123), "Blueberry");
fruits.put(new Integer(124), "Melon");
fruits.put(new Integer(125), "Rasberry");
Set dataset = fruits.entrySet();
Iterator iterate = dataset.iterator();
while(iterate.hasNext())
{
Map.Entry map=(Map.Entry)iterate.next();
System.out.println(map.getKey()+" "+map.getValue());
}
}
}
Output:
125 Rasberry
124 Melon
201 Kiwi
123 Blueberry
101 Orange
121 Cherry
Properties class:
The Properties class extends the Hashtable class in order to keep track of the list of values. The list has both a string key and a string value. You can define a default property in the Properties class that will be returned if no value is associated with a specific key.
Properties class Constructor :
- Properties(): It’s used to construct a Properties object that doesn’t have any default values.
- Properties(Properties propdefault): It is used to create the Properties object with the supplied property type parameter as its default value.
Example:
import java.util.*;
public class Coderz
{
public static void main(String[] args)
{
Properties probj = new Properties();
probj.put("Java", " May 23, 1995; 27 years ago");
probj.put("C++", "1985; 37 years ago");
probj.put("C", " 1972; 50 years ago");
probj.put("Python", " 20 February 1991; 31 years ago");
Set< ?> creator = probj.keySet();
for(Object ob: creator)
{
System.out.println(ob+" First appeared "+ probj.getProperty((String)ob) );
}
}
}
Output:
Java First appeared May 23, 1995; 27 years ago
C++ First appeared 1985; 37 years ago
C First appeared 1972; 50 years ago
Python First appeared 20 February 1991; 31 years ago
Stack Class:
The Stack class extends the Vector class, which has members that follow the LIFO (LAST IN FIRST OUT) principle. The stack implementation only has one default constructor, Stack ().
Example:
import java.util.*;
class Coderz {
public static void main(String args[])
{
Stack stobj = new Stack();
stobj.push(10);
stobj.push(9);
stobj.push(8);
stobj.push(7);
stobj.push(6);
Enumeration eobj = stobj.elements();
while(eobj.hasMoreElements())
System.out.print(eobj.nextElement()+" ");
stobj.pop();
stobj.pop();
System.out.println("\nAfter popping out two elements");
Enumeration eobj2 = stobj.elements();
while(eobj2.hasMoreElements())
System.out.print(eobj2.nextElement()+" ");
}
}
Output:
10 9 8 7 6
After popping out two elements
10 9 8
Dictionary Class:
The Dictionary class is similar to Map in that it represents the key/value storage repository. The Dictionary class is an abstract class that holds data in the form of a key/value pair. The dictionary can be defined as a collection of key/value pairs.
Although it is not currently deprecated, Dictionary is considered obsolete because it has been completely overtaken by the Map class.
Note: also read about the Comparator Interface in Java Collection Framework
Follow Me
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Leave a Comment