• Java Data Structures Tutorial

Retrieving values in a Dictionary



You can retrieve the value of a particular key using the get() method of the Dictionary class. If you pass the key of a particular element as a parameter of this method, it returns the value of the specified key (as an object). If the dictionary doesn’t contain any element under the specified key it returns null.

You can retrieve the values in a dictionary using this method.

Example

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

public class RetrievingElements {
   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);      
      Object ob = dic.get("Ram");
      System.out.println("Value of the specified key :"+ ob);            
   }
}

Output

Value of the specified key :94.6
Advertisements