Java Legacy Collection framework

When SUN Micro Systems has developed java, collection framework was known as data structures. Data structures in java were unable to meet industry requirements at that time. Hence data structures of java was reengineered and they have added ‘n’ number of classes and interfaces and in later stages the data structures of java is known as new collection framework we have one interface and classes.

Interface:
We have only one interface, namely java.util.Enumeration. This interface is used for  extracting the data from legacy collection framework classes.

Classes:
As a part of legacy collection framework we have the following essential classes: Vector, Stack, Dictionary, Hashtable and properties. Here, Vector and Stack belongs to one dimensional classes whereas Dictionary, Hashtable and Properties belongs to two dimensional classes.

What is the difference between normal collection framework and legacy collection framework?
Answer: All the classes in the normal collection framework are by default belongs to nonsynchronized classes whereas all classes in legacy collection framework are by default belongs to synchronized classes.

Vector:
Its functionality is exactly similar to ArrayList but Vector class belongs to synchronized whereas ArrayList belongs to non-synchronized class.

Creating a Vector is nothing but creating an object of java.util.Vector class.

Vector API:

Constructors:
Vector (); 1
Vector (int size); 2

Instance methods:
public int size (); -> 3
public void addItem (Object obj); [old] -> 4
public void addElement (Object obj); [new] -> 5
public void addItem (int pos, Object obj); -> 6
public void addElement (int pos, Object obj); -> 7
public Object getItem (int pos); -> 8
public Object remove (int pos); -> 9
public void remove (Object obj ); -> 10
public void removeAll (); -> 11
public Enumeration elements (); -> 12

The methods 4,5,6 and 7 are used for adding an object to the vector either at end or at specified position. Method-12 is used for extracting the data from vector object.

Enumeration en=v.elements ();

While (en.hasMoreElements ())
{
Object obj=en.nextElement ();
System.out.println (obj);
}

Java program which listed the concept of Vector?

import java.util.*;
class vector
{
public static void main (String [] args)
{
Vector v=new Vector ();
v.addElement (new Integer (10));
v.addElement (new Float (100.37f));
v.addElement (new Boolean (true));
v.addElement ("K.V.R");
System.out.println ("SIZE = "+v.size ());
System.out.println ("CONTENTS = "+v);
Enumeration e
while (en.hasMoreElements ())
{
Object val=en.nextElement ();
System.out.println (val);
}
}
};

Methods in Enumeration method

public boolean hasMoreElements ();
public Object nextElement ();

Method-1 is used for checkifalse when we have next element otherwise it returns false. Method element. Method-2 can be used as long as method

Stack: Stack is the sub-class of Vector class. The basic working principal of Stack is Last In First Out.

Stack API:

Constructors:
Stack ();
Stack (int size);

Instance methods:
public boolean empty (); 
public void push (Object);
public Object pop ();  -> 3
public Object peek (); -> 4
public int search (Object); -> 5

Method-1 returns true when the Stack does not contain any elements otherwise false. Method-2 is used for inserting an object into the Stack. Method-3 is used to remove top most elements permanently. Method-4 is used to retrieve top most elements without removing. Method-5 returns relative position of the element, if it found otherwise returns -1.

java program which illustrates the concept of Stack?

import java.util.*;
class stack
{
public static void main (String [] args)
{
Stack st=new Stack ();
System.out.println ("IS STACK EMPTY ? "+st.empty ());
System.out.println (st);
st.push (new Integer (10));
st.push (new Integer (20));
st.push (new Integer (30));
st.push (new Integer (40));
System.out.println (st);
System.out.println ("TOP MOST ELEMENT = "+st.peek ());
System.out.println (st);
System.out.println ("DELETED ELEMENT = "+st.pop ());
System.out.println ("MODIFIED STACK = "+st.peek ());
System.out.println ("IS 10 FOUND ? "+st.search (new Integer (10)));
Enumeration en=st.elements ();
while (en.hasMoreElements ())
{
Object obj=en.nextElement ();
System.out.println (obj);
}
}
};

Author: Krishna

Any Doubts. Please add comments and we will revert to that.

Leave a Reply