用于比较两个地图 ID 字段列表以查找缺失 ID 的 Lambda

Lambda for comparing two lists of map id fields for missing ids

我有两个地图列表,每个地图都是一个 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());

我想这让我得到了两个可以比较的字符串列表,但似乎这不是最佳方法。感谢任何帮助。

如果要过滤 collectionA 中不存在于 collectionB 中的项目映射,请迭代 collectionA 并检查每个条目是否存在于任何 [=14] 中=] 在 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());