La carte Java obtient la clé de la valeur
import java.util.HashMap;
import java.util.Map;
class Main
{
public static <K, V> K getKey(Map<K, V> map, V value)
{
return map.entrySet().stream()
.filter(entry -> value.equals(entry.getValue()))
.findFirst().map(Map.Entry::getKey)
.orElse(null);
}
public static void main(String[] args)
{
Map<String, Integer> hashMap = new HashMap();
hashMap.put("A", 1);
hashMap.put("B", 2);
hashMap.put("C", 3);
System.out.println(getKey(hashMap, 2)); // prints `B`
}
}
abdullah