Pages

Legacy Classes

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 :
  1. Dictionary
  2. HashTable
  3. Properties
  4. Stack
  5. Vector
NOTE: There is only one legacy interface called Enumeration. All the legacy classes are synchronized.

Dictionary Class

Dictionary Class is available in java.util package. The Dictionary class is the abstract parent of any class, such as Hashtable, which maps keys to values. In Dictionary class every key and every value is an object and every object key is associated with at most one value.

Dictionary Class Example :

import java.util.*;
public class DictionaryDemo {
      public static void main(String[] args) {
            Dictionary geek = new Hashtable();
            geek.put("123", "Code");
            geek.put("456", "Program");
            for (Enumeration i = geek.elements(); i.hasMoreElements();) {
                  System.out.println("Value in Dictionary : " + i.nextElement());
            }
            System.out.println("\nValue at key = 6 : " + geek.get("6"));
            System.out.println("Value at key = 456 : " + geek.get("123"));
            System.out.println("\nThere is no key-value pair : " + geek.isEmpty() + "\n");
            for (Enumeration k = geek.keys(); k.hasMoreElements();) {
                  System.out.println("Keys in Dictionary : " + k.nextElement());
            }
            System.out.println("\nRemove : " + geek.remove("123"));
            System.out.println("Check the value of removed key : " + geek.get("123"));
            System.out.println("\nSize of Dictionary : " + geek.size());
      }
}

HashTable Class

HashTable Class is available in java.util package. The HashTable class implements a hashtable, which maps keys to values. In this any non-null object can be used as a key or as a value. If many entries are to be made into a Hashtable, creating it with a sufficiently large capacity may allow the entries to be inserted more efficiently than letting it perform automatic rehashing as needed to grow the table.

HashTable Class Example :

import java.util.*; public class hashTabledemo {       public static void main(String[] args) {
            Hashtable h = new Hashtable();
            Hashtable h1 = new Hashtable();
            h.put(3, "Geeks");
            h.put(2, "forGeeks");
            h.put(1, "isBest");
            h1 = (Hashtable)h.clone();
            System.out.println("values in clone: " + h1);
            h.clear();
            System.out.println("after clearing: " + h);
      }
}

Properties Class

Properties Class is available in java.util package. The Properties class is a class which represents a persistent set of properties.The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string. A property list can contain another property list as its 'defaults', this second property list is searched if the property key is not found in the original property list. This class is thread-safe; multiple threads can share a single Properties object without the need for external synchronization.

Properties Class Example :

import java.util.*;
public class PropertiesDemo {
      public static void main(String[] args) {
            Properties gfg = new Properties();
            Set URL;
            String str;
            gfg.put("ide", "ide.geeksforgeeks.org");
            gfg.put("contribute", "contribute.geeksforgeeks.org");
            gfg.put("quiz", "quiz.geeksforgeeks.org");
            URL = gfg.keySet();
            Iterator itr = URL.iterator();
            while(itr.hasNext()) {
                  str = (String)itr.next();
                  System.out.println("The URL for " + str + " is " + gfg.getProperty(str));
            }
            System.out.println();
            str = gfg.getProperty("articl", "not found");
            System.out.println("The URL for article is " + str);
      }
}

Stack Class

Stack Class is available in java.util package. The Stack class represents a last-in-first-out (LIFO) stack of objects. When a stack is first created, it contains no items. In this class, the last element inserted is accessed first.

Stack Class Example :

import java.io.*;
import java.util.*;
public class StackDemo {
      static void stack_push(Stack stack) {
            for(int i = 0; i < 5; i++) {
                  stack.push(i);
            }
      }
      static void stack_pop(Stack stack) {
            System.out.println("Pop :");
            for(int i = 0; i < 5; i++) {
                  Integer y = (Integer) stack.pop();
                  System.out.println(y);
            }
      }
      static void stack_peek(Stack stack) {
            Integer element = (Integer) stack.peek();
            System.out.println("Element on stack top : " + element);
      }
      static void stack_search(Stack stack, int element) {
            Integer pos = (Integer) stack.search(element);
            if(pos == -1)
                  System.out.println("Element not found");
            else
                  System.out.println("Element is found at position " + pos);
      }
      public static void main(String[] args) {
            Stack stack = new Stack();
            stack_push(stack);
            stack_pop(stack);
            stack_push(stack);
            stack_peek(stack);
            stack_search(stack, 2);
            stack_search(stack, 6);
      }
}

Vector Class

Vector Class is available in java.util package. The Vector class implements a growable array of objects. Similar to an Array, it contains components that can be accessed using an integer index. The size of a Vector can grow or shrink as needed to accommodate adding and removing items. Each vector tries to optimize storage management by maintaining a capacity and a capacityIncrement. As of the Java 2 platform v1.2, this class was retrofitted to implement the List interface. Unlike the new collection implementations, Vector is synchronized. This class is a member of the Java Collections Framework.

Vector Class Example :

import java.util.*;
public class VectorDemo {
      public static void main(String[] args) {
            Vector v = new Vector();
            v.add(1);
            v.add(2);
            v.add("geeks");
            v.add("forGeeks");
            v.add(3);
            System.out.println("Vector is " + v);
      }
}