可以使用containsKey()方法来判断Map的key是否包含List中的一个值。

例如,假设有一个Map<String, Integer> map,和一个List list,你想要判断Map的key是否包含List中的一个值。

你可以使用如下的代码来实现:

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

// 添加一些元素到map和list中
map.put("key1", 1);
map.put("key2", 2);
list.add("key1");
list.add("key3");

// 判断map的key是否包含list中的一个值
for (String key : list) {
    if (map.containsKey(key)) {
        System.out.println("Map的key包含List中的值:" + key);
    } else {
        System.out.println("Map的key不包含List中的值:" + key);
    }
}

运行上述代码,将会输出:

Map的key包含List中的值:key1
Map的key不包含List中的值:key3

这样就可以判断Map的key是否包含List中的一个值了

java的map的key判断是否包含list中的一个值

原文地址: http://www.cveoy.top/t/topic/h8g9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录