• Java Data Structures Tutorial

Java Data Structures - Creating Dictionary



Since Dictionary is an abstract class you cannot instantiate it directly. The HashTable class is the sub class of the class Dictionary. Therefore, You can create a dictionary object by instantiating this class.

Dictionary dic = new Hashtable();

Example

import java.util.Hashtable;
import java.util.Dictionary;

public class CreatingDictionary {
   public static void main(String args[]) {
      Dictionary dic = new Hashtable();
      dic.put("Ram", 94.6);
      dic.put("Rahim", 92);
      dic.put("Robert", 85);
      dic.put("Roja", 93);
      dic.put("Raja", 75);
      System.out.println(dic);    		  
   }
}

Output

{Rahim = 92, Roja = 93, Raja = 75, Ram = 94.6, Robert = 85}
Advertisements