在Java中通过值从Map中删除元素的最快方法是什么? [英] What's the quickest way to remove an element from a Map by value in Java?

查看:1244
本文介绍了在Java中通过值从Map中删除元素的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过Java中的值从地图中删除元素的最快方法是什么?

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

目前我使用的是:

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


推荐答案

地图( commons-collections google集合 a>有它们),你会被困扰着迭代Map

Without using a Bi-directional map (commons-collections and google collections have them), you're stuck with iterating the Map

这篇关于在Java中通过值从Map中删除元素的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆