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

后端 未结 12 565
臣服心动
臣服心动 2020-12-09 01:36

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

Currently I\'m using:

    DomainObj valueToRemove = new DomainObj();
    S         


        
12条回答
  •  爱一瞬间的悲伤
    2020-12-09 01:51

    Here's the one-line solution:

    map.values().remove(valueToRemove);
    

    That's probably faster than defining your own iterator, since the JDK collection code has been significantly optimized.

    As others have mentioned, a bimap will have faster value removes, though it requires more memory and takes longer to populate. Also, a bimap only works when the values are unique, which may or may not be the case in your code.

提交回复
热议问题