hashmap get key by value code example
Example 1: find a value in hashmap
if(hashMap.containsKey(key)) {
Object o = hashMap.get(key);
}
Example 2: 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 3: java hashmap get array of keys
for (int key : myHashMap.keySet()){
}
Example 4: hashmap get value by key java
import java.util.HashMap;
HashMap<Int, String> examplehashmap=new HashMap<>();
{
examplehashmap.put(5, "example");
};
examplehashmap.get(5);