What's the quickest way to remove an element from a Map by value in Java?
Currently I'm using:
DomainObj valueToRemove = new DomainObj(); String removalKey = null; for (Map.Entry<String, DomainObj> entry : map.entrySet()) { if (valueToRemove.equals(entry.getValue())) { removalKey = entry.getKey(); break; } } if (removalKey != null) { map.remove(removalKey); }
Without using a Bi-directional map (commons-collections and google collections have them), you're stuck with iterating the Map
1.4m articles
1.4m replys
5 comments
57.0k users