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

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

    A shorter usage of iterator is to use a values() iterator.

    DomainObj valueToRemove = new DomainObj();
    for (Iterator it = map.values().iterator(); it.hasNext();)) {
            if (valueToRemove.equals(it.next())) {
                    it.remove();
                    break;
            }
    }
    

提交回复
热议问题