java get hashmap keyset code example

Example 1: hashmap get value 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");

Example 2: access each key and value in a hashmap java

// access each key and corresponding value in a hashmap
HashMap<Integer, Integer> map = new HashMap<>();

for(Map.Entry<Integer, Integer> entry : map.entrySet()){
  entry.getKey();
  entry.getValue();
}