Java HashMap.get(Object key)

Syntax

HashMap.get(Object key) has the following syntax.

public V get(Object key)

Example

In the following code shows how to use HashMap.get(Object key) method.


//from   w  ww  . j  a v a 2  s  .  c  om


import java.util.HashMap;

public class Main {
   public static void main(String args[]) {
      HashMap<Integer, String> newmap = new HashMap<Integer, String>();      
      
      // populate hash map
      newmap.put(1, "tutorials");
      newmap.put(2, "from");
      newmap.put(3, "java2s.com");
      
      // get value of key 3
      String val=(String)newmap.get(3);
      
      // check the value
      System.out.println("Value for key 3 is: " + val);
   }    
}

The code above generates the following result.