[edit] Java Legacy Classes and Interfaces
There is one Legacy Interface called as Enumeration. This interface defines the methods by which we can enumerate the elements in a collection of objects. It is used by several methods defined by the legacy classes.
The Legacy Classes which are defined by Java.utilare discussed below :
1. Dicitionary
2. Hashtable
3. Properties
4. Vector
5. Stack
The brief explanation about each of these five classes are taken as under:
1. Dictionary : It is an abstract class that denotes a key or a value storage space very much like the Map.Here we can store the value in the dictionary instead of the Map if we have the key and the value of it. And we can anytime retrieve this value with the help pf this unique key. It is basically considered as Obsolete as all its features are available in the improved and advanced form in the form of a Map.
We can use put ( ) method to add a key and vaule , a get ( ) method to find the value for any key. The keys and the values can be returned as an enumeration by the keys ( ) method and elements ( ) method. We can also delete the key or valu pair by using the remove ( ) method.
2. Hashtable : It is very much similar to hashMap if compared as it also stores the key/value pairs in the table. While Using this we can mention an object that is used as a key, and the value that we want linked to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table.
A hash table can only store objects that override the hashCode( ) and equals( ) methods that are defined by Object. The hashCode( ) method must compute and return the hash code for the object.
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
public class HashtableExample {
public static void main(String[] args) {
Hashtable table = new Hashtable();
table.put("1", "Pacific Ocean");
table.put("2", "Atlantic Ocean");
table.put("3", "India Ocean");
table.put("4", "Arctic Ocean");
Set keySet = table.keySet();
for(Iterator iterator = keySet.iterator(); iterator.hasNext();) {
String oceanName = (String) iterator.next();
System.out.println(table.get(oceanName));
}
}
}
3. Properties: It is a subclass of Hashtable. It is used to maintain lists of values in which the key is a String and the value is also a String. It defines the following instance Variable:
Properties defaults;
It holds a default property list which is related with a Properties Object.
Properties defines these two constructors:
Properties( )
Properties(Properties propDefault)
Properties ( ): this creates a Properties object that has no default values.
Properties(Properties propDefault) : It creates an object that uses propDefault for its default values.
We can specify a default property that will be returned if no value is associated with a certail key.
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
public class PropertiesExample {
public static void main(String[] args) throws FileNotFoundException, IOException {
FileInputStream inputStream = new FileInputStream("example.properties");
Properties properties = new Properties();
properties.load(inputStream);
System.out.println(properties.get("example3"));
System.out.println(properties.get("example1"));
}
}
example.properties
example1=Base Ball
example2=Cricket
example3=Hockey
example4=Football
example5=Rugby
example6=Lawn Tennis
[edit] Java Vectors
It implemnts a dynamic array very much similar to the ArrayList.It has few differences as it is synchronized and conatins many legacy methods. It basically extends the AbstractList Class and implemnts the List Interface.
All vectors start with an initial capacity. After this initial capacity is reached, the next time that when we attempt to store an object in the vector, the vector automatically allocates space for that object plus extra room for additional objects. By allocating more than just the required memory, the vector reduces the number of allocations that must take place. This reduction is important, because allocations are costly in terms of time. The amount of extra space allocated during each reallocation is determined by the increment that we specify at the time of creating the vector. If we don’t specify an Increment, the vector’s size is doubled by each allocation cycle. The increment value is stored in capacityIncrement. The number of elements currently in the vector is stored in elementCount. The array that holds the vector is stored in elementData.
Few Vector constructors are:
Vector( )
Vector(int size)
Vector(int size, int incr)
Vector(Collection c)
Each one of them is briefed as under:
Vector( ) : It creates a default vectorand this vector has the initial size of 10.
Vector(int size): It creates a vector whose initial capacity is specified by size.
Vector(int size, int incr) : It creates a vector whose initial capacity is specified by size and whose increment is specified by incr. The increment specifies the number of elements to allocate each time that a vector is moved upward.
Vector(Collection c): It creates a vector that contains the elements of collection c.
import java.util.Iterator;
import java.util.Vector;
public class VectorExample {
public static void main(String[] args) {
Vector vector = new Vector();
vector.add("Pacific Ocean");
vector.add("Atlantic Ocean");
vector.add("India Ocean");
vector.add("Arctic Ocean");
for(Iterator iterator = vector.iterator(); iterator.hasNext();) {
String oceanName = (String) iterator.next();
System.out.println(oceanName);
}
}
}
5. Stack: It is a sub class of Vector which implements a standard last-in, first-out stack.It defines only a default constructor which creates an empty Stack and it includes all the methods defined by vector. We can call pop ( ) method to remove and return the top. An EmptyStackException is thrown if you call pop( ) when the invoking stack is empty. We can use peek( ) 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. Here is an example that creates a stack, pushes several Integer objects onto it, and then pops them off again:
|