java hashmap getting key by value code example

Example 1: java get keys from hashmap

//returns as set
hashmap.keySet()

Example 2: java hashmap get value

package com.tutorialspoint;

import java.util.*;

public class HashMapDemo {
   public static void main(String args[]) {
      
      // create hash map
      HashMap newmap = new HashMap();

      // populate hash map
      newmap.put(1, "tutorials");
      newmap.put(2, "point");
      newmap.put(3, "is best");

      // get value of key 3
      String val = (String)newmap.get(3);

      // check the value
      System.out.println("Value for key 3 is: " + val);
   }    
}

Tags:

Java Example