Java Hashtable get(Object key) Example

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

Signature

public V get(Object key)

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

Example

The following example shows the usage of java.util.Hashtable.get(key) method.

import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

public class GetMethodTreeSetExample 
{
    public static void main(String args[])
    {
        // create an empty Hashtable
        Hashtable<Integer,String>  hashtable1 = new Hashtable<Integer,String>();
        
        // use put() method to put elements to the Hashtable
        hashtable1.put(1,"Element1");
        hashtable1.put(2,"Element2");
        hashtable1.put(3,"Element3");
        hashtable1.put(4,"Element4");
        hashtable1.put(5,"Element5");
        
        //get the value of key 2
        String val = (String)hashtable1.get(2)
        System.out.println("Value of Key \"2\" is : "+val);
    }
}

Output

Value of Key 2 : Element2

Leave a Reply

Your email address will not be published. Required fields are marked *