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

Posted by Supertux on Stack Overflow See other posts from Stack Overflow or by Supertux
Published on 2009-08-26T16:23:23Z Indexed on 2010/04/29 9:57 UTC
Read the original article Hit count: 298

Filed under:
|
|

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);
	}

© Stack Overflow or respective owner

Related posts about java

Related posts about Performance