Sunday, 21 December 2014

What is legacy classes in Java ?

Legacy Elements:

Before Collection Framework, defined several classes and interface that provide method for storing objects. When Collection framework were added in J2SE 1.2, the original classes were reengineered(included) to support the collection interface. These classes are also known as Legacy classes.All legacy claases and interface were redesign by JDK 5 to support Generics.

The following are the legacy classes defined by java.util package.

1. Dictionary
2. HashTable
3. Properties
4. Stack
5. Vector

There is only one legacy interface called Enumeration.

Note: All the legacy classes are syncronized.

Enumeration interface:

1. Enumeration interface defines method to enumerate through collection of object.
2. This interface is suspended by Iterator interface.
3. However some legacy classes such as Vector and Properties defines several method in which
    Enumeration interface is used.
4. It specifies the following two methods

   boolean hasMoreElements()
   Object nextElement()

Vector class:

1. Vector is similar to ArrayList which represents a dynamic array.
2. The only difference between Vector and ArrayList is that Vector is synchronised while Array is           not.
3. Vector class has following four constructor

Vector()
Vector(int size)
Vector(int size, int incr)
Vector(Collection< ? extends E> c)

Vector defines several legacy method.
Lets see some important legacy method define by Vector class.

addElement()--> add element to the Vector
elementAt()---> return the element at specified index
elements()----> return an enumeration of element in vector
firstElement()-> return first element in the Vector
lastElement()--> return last element in the Vector
removeAllElement()--> remove all element of the Vector

Example of Vector:

import java.util.*;
public class Test
{
public static void main(String []args)
{
Vector v1 = new Vector();
v1.add(40);
v1.add(50);
v1.add(60);
v1.add(70);
v1.add(80);
v1.add(90);

Enumeration en = ve.elements();
while(en.hasMoreElements())
    {
System.out.println(en.nextElement());
     }
  }
}

Output: 
40
50
60
70
80
90

Hashtable class:

1. Like HashMap, Hashtable also stores key/value pair in hashtable. However neither keys nor values       can be null.
2. There is one more difference between HashMap and Hashtable that is Hashtable is synchronized           while  HashMap is not.
3. Hashtable has following four constructor

Hashtable()
Hashtable(int size)
Hashtable(int size, float fillratio)
Hashtable(Map< ? extends K, ? extends V> m)

Example of Hashtable;

import java.util.*;
class HashTableExample
{
public static void main(String []args)
{
Hashtable< String,Integer> ht = new Hashtable< String,Integer>();
ht.put("a",new Integer(400));
ht.put("b",new Integer(300));
ht.put("c",new Integer(200));
ht.put("d",new Integer(100));

Set st = ht.entrySet();
Iterator itr=st.iterator();
while(itr.hasNext())
     {
Map.Entry m=(Map.Entry)itr.next();
System.out.println(itr.getKey()+" "+itr.getValue());
     }
  }
}

Output: 
a 400
b 300
c 200
d 100

Properties class:

1. Properties class extends Hashtable class.
2. It is used to maintain list of value in which both key and value are String
3. Properties class define two constructor

    Properties()
    Properties(Properties default)
4. 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.


Example of Properties class;

import java.util.*;
public class Test
{
public static void main(String []args)
{
Properties p = new Properties();
Properties p = new Properties();
p.put("Java", "James Ghosling");
p.put("C++", "Bjarne Stroustrup");
p.put("C", "Dennis Ritchie");
p.put("C#", "Microsoft Inc.");

Set set = p.keySet();
for(Object ob: set)
     {
System.out.println(ob+" was created by "+ p.getProperty((String)ob) );
    }
  }
}

Output: 
Java was created by James Ghosling
C++ was created by Bjarne Stroustrup
C was created by Dennis Ritchie
C# was created by Microsoft Inc

No comments:

Post a Comment