可以将Map <Integer,ArrayList <String >>与ArrayList <String>的值进行比较

I created a Map<Integer, ArrayList<String>> map and I would like to compare each value in map with one ArrayList<String> likeList and get key if they match. I will bring the key to use later.

我试图像这样运行我的代码,但是它不起作用,因为它什么也不返回:

for (int key : map.keySet()) {
    if(map.get(key).equals(likeList)){
        index = key;
        Log.d("IndexN", String.valueOf(index));
    }
}

然后,我尝试了这个:

int index = 0;
for (Map.Entry<Integer, ArrayList<String>> entry : map.entrySet()) {
    if(entry.getValue().equals(likeList)){
        index = entry.getkey();
    }
}

你有什么主意吗?

评论
上个路口
上个路口

尝试这个。这只是流原始列表的entrySet和

  • 根据相等的值过滤到likeList和
  • 使用相似的值和原始键创建一个新地图
        List<String> list1 = List.of("a","b","c");
        List<String> list2 = List.of("a","b","c");
        List<String> list3 = List.of("a","c","b");
        Map<Integer,List<String>> oldMap = Map.of(1,list1,2,list2,3,list3);
        List<String> likeList = List.of("a","b","c");

        Map<Integer, List<String>> newMap = oldMap.entrySet().stream()
                .filter(e -> e.getValue().equals(likeList))
                .collect(Collectors.toMap(Entry::getKey,
                        Entry::getValue));

        newMap.entrySet().forEach(System.out::println);

版画

1=[a, b, c]
2=[a, b, c]

注意,列表的顺序很重要。

点赞
评论