我有清单:
ArrayList<String> list = new ArrayList<>();
和地图:
Map<String, List<String>> map = new HashMap<>();
我需要比较列表和map的值,并根据该值
返回键问题在于我不知道如何同步迭代,因为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));
..但即使有某些价值,这个电话也会重新调整错误......
任何想法如何获得每个值的每个键? 非常感谢你
答案 0 :(得分:2)
您正在将List<String>
与String
进行比较,因此它永远不会返回true。
使用List.contains
来确定String
中是否显示List
:
if (entry.getValue().contains(value)) {
return entry.getKey();
}