• Java Data Structures Tutorial

Adding elements to a Dictionary



The Dictionary class provides a method named put() this method accepts key-value pairs (objects) as parameters and adds them to a dictionary.

You can add elements to a dictionary using this method.

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