我有两个 map列表,每个 map都作为一个ID字段。我需要将这两个列表相互比较,以从collectionB中找到丢失的ID(下面的“ 7777”)
List<Map<String, Object>> collectionA = new ArrayList<Map<String, Object>>() {{
add(new HashMap<String, Object>() {{ put("id", "5555"); }});
add(new HashMap<String, Object>() {{ put("id", "6666"); }});
add(new HashMap<String, Object>() {{ put("id", "7777"); }});
add(new HashMap<String, Object>() {{ put("id", "8888"); }});
}};
List<Map<String, Object>> collectionB = new ArrayList<Map<String, Object>>() {{
add(new HashMap<String, Object>() {{
add(new HashMap<String, Object>() {{ put("id", "5555"); }});
add(new HashMap<String, Object>() {{ put("id", "6666"); }});
add(new HashMap<String, Object>() {{ put("id", "8888"); }});
}});
}};
我真的很想了解有关stream()的更多信息,因此对此提供的任何帮助将不胜感激。如你所知,我不太确定从哪里开始:
我开始走这条路,但似乎这不是正确的方法。
List<String> bids = collectionB.stream()
.map(e -> e.entrySet()
.stream()
.filter(x -> x.getKey().equals("id"))
.map(x -> x.getValue().toString())
.collect(joining("")
)).filter(x -> StringUtils.isNotEmpty(x)).collect(Collectors.toList());
我想这使我进入了两个可以比较的字符串列表,但这似乎不是最佳方法。任何帮助表示赞赏。
如果要筛选项目的 map,从collectionA
它们是不存在的collectionB
,迭代collectionA
和检查每个条目存在于任何的Map
中collectionB
,终于收集进入Map
是不存在的collectionB
List<Map<String,String>> results = collectionA.stream()
.flatMap(map->map.entrySet().stream())
.filter(entry->collectionB.stream().noneMatch(bMap->bMap.containsValue(entry.getValue())))
.map(entry-> Collections.singletonMap(entry.getKey(),entry.getValue()))
.collect(Collectors.toList());
谢谢,这极大地帮助了我。我不太清楚为什么);Collectors.toList(); 在“外部”工作/在其中粘贴分号。noneMatch很棒,我不知道那是那里。