What's the quickest way to remove an element from a Map by value in Java?

Viewed 70163

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);
    }
12 Answers
Related