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

后端 未结 12 581
臣服心动
臣服心动 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:56

    If you have no way to figure out the key from the DomainObj, then I don't see how you can improve on that. There's no built in method to get the key from the value, so you have to iterate through the map.

    If this is something you're doing all the time, you might maintain two maps (string->DomainObj and DomainObj->Key).

提交回复
热议问题