繁体   English   中英

如何将数组列表与Hasmap进行比较并根据值从Hashmap获取密钥?

[英]How to compare arraylist with Hasmap and get the key from Hashmap based on value?

我有清单:

ArrayList<String> list = new ArrayList<>();

和地图:

Map<String, List<String>> map = new HashMap<>();

我需要比较列表和地图的值,并根据该值返回键

问题是我不知道如何同步迭代,因为arraylist的大小比map中的每个列表小。

我也尝试了这种方法:

public static Object getKeyByValue(Map<String,List<String>> map, String value) {
    for (Entry<String, List<String>> entry : map.entrySet()) {
        if (Objects.equals(value, entry.getValue())) {
            return entry.getKey();
        }
    }
    return null;
}

getKeyByValue(map,list.get(0)); 

..但是即使有一定的价值,这个电话也被重新设置为假...

有什么想法如何获取每个值的每个键? 非常感谢你

您正在将List<String>String进行比较,因此它永远不会返回true。

改用List.contains确定String出现在List

    if (entry.getValue().contains(value)) {
        return entry.getKey();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM