Java Utility Library

Java Hashtable - get() Method



The java.util.Hashtable.get() method returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

Syntax

public V get(Object key)

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


Parameters

key Specify the key whose associated value is to be returned.

Return Value

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

Exception

Throws NullPointerException, if the specified key is null

Example:

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

import java.util.*;

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

    //populating hashtable
    Htable.put(101, "John");
    Htable.put(102, "Marry");
    Htable.put(103, "Kim");
    Htable.put(104, "Jo");

    //printing mapped value of specified key
    System.out.println("101 is mapped to: " + Htable.get(101));
    System.out.println("102 is mapped to: " + Htable.get(102));
    System.out.println("103 is mapped to: " + Htable.get(103));
    System.out.println("104 is mapped to: " + Htable.get(104));  
  }
}

The output of the above code will be:

101 is mapped to: John
102 is mapped to: Marry
103 is mapped to: Kim
104 is mapped to: Jo

❮ Java.util - Hashtable