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
There is only one legacy interface called Enumeration
NOTE: All the legacy classes are synchronized
boolean hasMoreElements() //It returns true while there are still more elements to extract,
and returns false when all the elements have been enumerated.
Object nextElement() //It returns the next object in the enumeration i.e. each call to nextElement() method
obtains the next object in the enumeration. It throws NoSuchElementException when the
enumeration is complete.
Vector() //This creates a default vector, which has an initial size of 10.
Vector(int size) //This creates a vector whose initial capacity is specified by size.
Vector(int size, int incr) //This 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
when a vector is resized for addition of objects.
Vector(Collection c) //This creates a vector that contains the elements of collection c.
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 |
import java.util.*;
public class Test
{
public static void main(String[] args)
{
Vector<Integer> ve = new Vector<Integer>();
ve.add(10);
ve.add(20);
ve.add(30);
ve.add(40);
ve.add(50);
ve.add(60);
Enumeration<Integer> en = ve.elements();
while(en.hasMoreElements())
{
System.out.println(en.nextElement());
}
}
}
10 20 30 40 50 60
Hashtable() //This is the default constructor. The default size is 11.
Hashtable(int size) //This creates a hash table that has an initial size specified by size.
Hashtable(int size, float fillratio) //This creates a hash table that has an initial size specified by size
and a fill ratio specified by fillRatio. This ratio must be between 0.0 and 1.0, and it determines how full
the hash table can be before it is resized upward. Specifically, when the number of elements is greater
than the capacity of the hash table multiplied by its fill ratio, the hash table is expanded.
If you do not specify a fill ratio, then 0.75 is used.
Hashtable(Map< ? extends K, ? extends V> m) //This creates a hash table that is initialized with the
elements in m. The capacity of the hash table is set to twice the number of elements in m.
The default load factor of 0.75 is used.
import java.util.*;
class HashTableDemo
{
public static void main(String args[])
{
Hashtable<String,Integer> ht = new Hashtable<String,Integer>();
ht.put("a",new Integer(100));
ht.put("b",new Integer(200));
ht.put("c",new Integer(300));
ht.put("d",new Integer(400));
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());
}
}
}
a 100 b 200 c 300 d 400
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() //This creates a Properties object that has no default values
Properties(Properties propdefault) //This creates an object that uses propdefault for its default values.
Note: In both cases, the property list is empty
import java.util.*;
public class Test
{
public static void main(String[] args)
{
Properties pr = new Properties();
pr.put("Java", "James Ghosling");
pr.put("C++", "Bjarne Stroustrup");
pr.put("C", "Dennis Ritchie");
pr.put("C#", "Microsoft Inc.");
Set< ?> creator = pr.keySet();
for(Object ob: creator)
{
System.out.println(ob+" was created by "+ pr.getProperty((String)ob) );
}
}
}
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() //This creates an empty stack
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.
import java.util.*;
class StackDemo {
public static void main(String args[]) {
Stack st = new Stack();
st.push(11);
st.push(22);
st.push(33);
st.push(44);
st.push(55);
Enumeration e1 = st.elements();
while(e1.hasMoreElements())
System.out.print(e1.nextElement()+" ");
st.pop();
st.pop();
System.out.println("\nAfter popping out two elements");
Enumeration e2 = st.elements();
while(e2.hasMoreElements())
System.out.print(e2.nextElement()+" ");
}
}
11 22 33 44 55 After popping out two elements 11 22 33