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:
Enumeration is the only remaining legacy interface.
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.
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 |
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());
}
}
}
101
201
103
42
501
116
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.
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());
}
}
}
125 Rasberry
124 Melon
201 Kiwi
123 Blueberry
101 Orange
121 Cherry
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.
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) );
}
}
}
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
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 ().
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()+" ");
}
}
10 9 8 7 6
After popping out two elements
10 9 8
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
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.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…