Java Utility Library

Java Dictionary - get() Method



The java.util.Dictionary.get() method returns the value to which the key is mapped in this dictionary. If the dictionary contains an entry for the specified key, the associated value is returned; otherwise, null is returned.

Syntax

public abstract V get(Object key)

Here, V is the type of value maintained by the container.


Parameters

key Specify a key in this dictionary.

Return Value

Returns the value to which the key is mapped in this dictionary. null if the key is not mapped to any value in this dictionary.

Exception

Throws NullPointerException, if the key is null.

Example:

In the example below, the java.util.Dictionary.get() method returns the value to which the key is mapped in the given dictionary.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a dictionary
    Dictionary<Integer, String> Dict = 
        new Hashtable<Integer, String>();

    //populating dictionary
    Dict.put(101, "John");
    Dict.put(102, "Marry");
    Dict.put(103, "Kim");
    Dict.put(104, "Sam");
    Dict.put(105, "Jo");

    //printing the content of the dictionary
    //using get method
    System.out.println("The value associated with 101 is: " + Dict.get(101));
    System.out.println("The value associated with 102 is: " + Dict.get(102));
    System.out.println("The value associated with 103 is: " + Dict.get(103));

  }
}

The output of the above code will be:

The value associated with 101 is: John
The value associated with 102 is: Marry
The value associated with 103 is: Kim

❮ Java.util - Dictionary