首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用Java8 lambda表达式遍历多个列表并通过合并两个列表中的唯一值来创建另一个列表

如何使用Java8 lambda表达式遍历多个列表并通过合并两个列表中的唯一值来创建另一个列表
EN

Stack Overflow用户
提问于 2018-09-26 03:39:03
回答 2查看 156关注 0票数 0

我正在编写一段代码,循环遍历多个列表,并使用Java8 lambda表达式合并来自两个列表的唯一值,从而创建另一个列表。

模型类:

代码语言:javascript
复制
class ServiceMap{
    Integer serviceMapId;
    Integer seviceId;
}

代码逻辑:

代码语言:javascript
复制
List<ServiceMap> listA = getServiceMaps();//Will get from database
List<Integer> listB = Arrays.asList(1, 10, 9);//Will get from client
List<ServiceMap> listC = new ArrayList<>();//Building it merging of both lists above

listA.stream().forEach(e -> {
    if (listB.parallelStream().noneMatch(x -> x == e.getServiceId())) {
        listC.add(new ServiceMap(e.getServiceId()));
        return;
    }

    listB.stream().forEach(x -> {
        if (listC.stream().anyMatch(e2->e2.getServiceId() == x)) {
            return;
        }
        if (x == e.getServiceId()) {
            listC.add(new ServiceMap(e.getServiceId()));
        } else {
            listC.add(new ServiceMap(x));
        }
    });

});
listC.stream().forEach(x -> System.out.println(x));

使用java lambda表达式编写代码是否有效?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52505591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档