Q:

hashmap get value by key java


//Import Hashmap
import java.util.HashMap;
   
    HashMap<String, String> dir = new HashMap<String, String>();
//Add key, values  
    dir.put("hi", "hello");
	dir.put("wow", "amazing");
//print value for hi.
    System.out.println(dir.get("hi");
2
import java.util.HashMap;
//Within a class
//You can do new HashMap<Key Type, Value Type>();, but you don't need to
HashMap<Int, String> examplehashmap=new HashMap<>();
{
//put in values
 examplehashmap.put(5, "example");
};
//get value
examplehashmap.get(5);
//returns "example"
7
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);
   }    
}
0
//returns as set
hashmap.keySet()
0

New to Communities?

Join the community