how to get value from key in hashmap in java code example
Example 1: java map get the key from value
public static <T, E> Set<T> getKeyByValue(Map<T, E> map, E value) {
return map.entrySet()
.stream()
.filter(entry -> Objects.equals(entry.getValue(), value))
.map(Map.Entry::getKey)
.collect(Collectors.toSet());
}
Example 2: java hashmap get value
package com.tutorialspoint;
import java.util.*;
public class HashMapDemo {
public static void main(String args[]) {
HashMap newmap = new HashMap();
newmap.put(1, "tutorials");
newmap.put(2, "point");
newmap.put(3, "is best");
String val = (String)newmap.get(3);
System.out.println("Value for key 3 is: " + val);
}
}