Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Java8Lambda表达式循环多个列表并通过合并两个列表中的唯一值创建另一个列表_Java_Lambda_Java 8_Java Stream_Distinct Values - Fatal编程技术网

如何使用Java8Lambda表达式循环多个列表并通过合并两个列表中的唯一值创建另一个列表

如何使用Java8Lambda表达式循环多个列表并通过合并两个列表中的唯一值创建另一个列表,java,lambda,java-8,java-stream,distinct-values,Java,Lambda,Java 8,Java Stream,Distinct Values,我正在编写一个代码来循环多个列表并创建另一个列表,使用Java8Lambda表达式合并两个列表中的唯一值 模型类: class ServiceMap{ Integer serviceMapId; Integer seviceId; } 代码逻辑: List<ServiceMap> listA = getServiceMaps();//Will get from database List<Integer> listB = Arrays.asList(1,

我正在编写一个代码来循环多个列表并创建另一个列表,使用Java8Lambda表达式合并两个列表中的唯一值

模型类:

class ServiceMap{
    Integer serviceMapId;
    Integer seviceId;
}
代码逻辑:

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));
List listA=getServiceMaps()//将从数据库中获取
listB=Arrays.asList(1,10,9)//将从客户处获得
List listC=new ArrayList()//将上面两个列表合并在一起构建it
listA.stream().forEach(e->{
if(listB.parallelStream().noneMatch(x->x==e.getServiceId()){
add(新的ServiceMap(例如getServiceId());
返回;
}
listB.stream().forEach(x->{
if(listC.stream().anyMatch(e2->e2.getServiceId()==x)){
返回;
}
如果(x==e.getServiceId()){
add(新的ServiceMap(例如getServiceId());
}否则{
添加(新服务地图(x));
}
});
});
stream().forEach(x->System.out.println(x));

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

您可以对每个列表进行流式处理,对其应用
distinct
,并收集:

List<Integer> result = 
    Stream.concat(listA.stream(), listB.stream())
          .distinct()
          .collect(Collectors.toList());
列表结果=
Stream.concat(listA.Stream(),listB.Stream())
.distinct()
.collect(Collectors.toList());

您可以对每个列表进行流式处理,对其应用
独特的
,并收集:

List<Integer> result = 
    Stream.concat(listA.stream(), listB.stream())
          .distinct()
          .collect(Collectors.toList());
列表结果=
Stream.concat(listA.Stream(),listB.Stream())
.distinct()
.collect(Collectors.toList());

你也应该这样使用

Stream<Integer> streamOfServiceMapIds = listA.stream().map(ServiceMap::getSeviceId);
List<ServiceMap> collectedList = Stream.concat(streamOfServiceMapIds, listB.stream())
        .distinct()
        .map(ServiceMap::new)
        .collect(Collectors.toList());
Stream-streamofservicecemapids=listA.Stream().map(ServiceMap::getSeviceId);
List collectedList=Stream.concat(streamofservicecemapids,listB.Stream())
.distinct()
.map(ServiceMap::新建)
.collect(Collectors.toList());

你也应该这样使用

Stream<Integer> streamOfServiceMapIds = listA.stream().map(ServiceMap::getSeviceId);
List<ServiceMap> collectedList = Stream.concat(streamOfServiceMapIds, listB.stream())
        .distinct()
        .map(ServiceMap::new)
        .collect(Collectors.toList());
Stream-streamofservicecemapids=listA.Stream().map(ServiceMap::getSeviceId);
List collectedList=Stream.concat(streamofservicecemapids,listB.Stream())
.distinct()
.map(ServiceMap::新建)
.collect(Collectors.toList());

Stream.of(listA,listB).map(Collection::Stream).reduce(Stream.empty(),Stream::concat).distinct().collector(Collectors.toList())
。你的解决方案肯定不正确。这不是更好吗<代码>集合=新的HashSet(listA);listB.stream().filter(i->!set.contains(i)).forEach(set::add)@BoristheSpider这有可怕的性能,虽然…@BoristheSpider我实际上误读了你的代码,我的坏。您的版本的一个小改进是
Stream.of(listA,listB).map(List::Stream).reduce(Stream::concat).orElse(Stream.empty()).distinct().collect(Collectors.toList())但是,为什么不
Stream.concat(listA,listB)
直接…@Krish你发布了代码,收到了我们花时间阅读和理解你的代码和帖子的评论和答案。事后更改代码是不可接受的。请回滚您的编辑。
Stream.of(listA,listB).map(Collection::Stream).reduce(Stream.empty(),Stream::concat).distinct().Collection(Collectors.toList())
。你的解决方案肯定不正确。这不是更好吗<代码>集合=新的HashSet(listA);listB.stream().filter(i->!set.contains(i)).forEach(set::add)@BoristheSpider这有可怕的性能,虽然…@BoristheSpider我实际上误读了你的代码,我的坏。您的版本的一个小改进是
Stream.of(listA,listB).map(List::Stream).reduce(Stream::concat).orElse(Stream.empty()).distinct().collect(Collectors.toList())但是,为什么不
Stream.concat(listA,listB)
直接…@Krish你发布了代码,收到了我们花时间阅读和理解你的代码和帖子的评论和答案。事后更改代码是不可接受的。请回滚您的编辑。