e28fd3e7c9
update to use the givenX_whenY_thenZ naming convention for tests Co-Authored-By: KevinGilmore <kpg102@gmail.com>
45 lines
1.0 KiB
Java
45 lines
1.0 KiB
Java
/**
|
|
*
|
|
*/
|
|
package com.baeldung.map;
|
|
|
|
import java.util.HashSet;
|
|
import java.util.Map;
|
|
import java.util.Map.Entry;
|
|
import java.util.Set;
|
|
import java.util.stream.Stream;
|
|
|
|
/**
|
|
* @author swpraman
|
|
*
|
|
*/
|
|
public class MapUtil {
|
|
|
|
public static <K, V> Stream<K> keys(Map<K, V> map, V value) {
|
|
return map.entrySet()
|
|
.stream()
|
|
.filter(entry -> value.equals(entry.getValue()))
|
|
.map(Map.Entry::getKey);
|
|
}
|
|
|
|
public static <K, V> Set<K> getKeys(Map<K, V> map, V value) {
|
|
Set<K> keys = new HashSet<>();
|
|
for (Entry<K, V> entry : map.entrySet()) {
|
|
if (entry.getValue().equals(value)) {
|
|
keys.add(entry.getKey());
|
|
}
|
|
}
|
|
return keys;
|
|
}
|
|
|
|
public static <K, V> K getKey(Map<K, V> map, V value) {
|
|
for (Entry<K, V> entry : map.entrySet()) {
|
|
if (entry.getValue().equals(value)) {
|
|
return entry.getKey();
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
}
|