A map maps a key to a value. If you have a value and you know the map contains this value, why do you need the key anymore?
On the other hand, if you really need the key or you have just a property of the value, you can iterate the entrySet()
, check the value and return the key if found:
for (Map.Entry<Index,Value> entry : map.entrySet()) {
if (entry.getValue().getXy().equals(xy)) {
return entry.getKey();
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…