Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.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 - Fatal编程技术网

循环多个不同类型的列表,并使用Java8Lambda表达式创建另一个列表

循环多个不同类型的列表,并使用Java8Lambda表达式创建另一个列表,java,lambda,java-8,java-stream,Java,Lambda,Java 8,Java Stream,我正在编写一个代码来循环遍历多个不同类型的列表,并使用Java8Lambda表达式基于一些业务验证创建另一个列表 业务验证/逻辑: there are 2 lists List<ServiceMap> listA and List<Integer> listB 1. if the element in listA not present in listB then deactivate that record in DB. 2. if the element in lis

我正在编写一个代码来循环遍历多个不同类型的列表,并使用Java8Lambda表达式基于一些业务验证创建另一个列表

业务验证/逻辑:

there are 2 lists List<ServiceMap> listA and List<Integer> listB
1. if the element in listA not present in listB then deactivate that record in DB.
2. if the element in listA present in listB then activate that record in DB.
3. if the element in listB not present in listA then create new record in DB.
代码:

List listA=getServiceMaps()//将从数据库中获取
listB=Arrays.asList(1,10,9)//将从客户处获得
List listC=new ArrayList()//通过验证上面两个列表中的记录来构建新列表
listA.stream().forEach(e->{
//非匹配意味着我们必须停用数据库中的记录
if(listB.parallelStream().noneMatch(x->x==e.getServiceId()){
ServiceMap recordToDeactivate=e;
e、 setIsActive(假);
添加(recordToDeactivate);
返回;
}
listB.stream().forEach(x->{
//如果该记录已添加到listC,则继续下一个记录
if(listC.stream().anyMatch(e2->e2.getServiceId()==x)){
返回;
}
//匹配意味着我们必须激活数据库中的记录
如果(x==e.getServiceId()){
ServiceMap recordToActivate=e;
e、 setIsActive(真);
添加(记录激活);
返回;
}否则{
//不匹配表示在数据库中创建新记录
ServiceMap newM=新ServiceMap();
newM.setIsActive(真);
newM.setServiceId(x);
列表c.add(newM);
}
});
});

有什么方法可以用最简单的方式实现上述逻辑吗

不知道为什么要为列表A中的每个项目通过列表B进行流式处理,也不知道为什么要使用流式处理

List<Integer> notInA = new ArrayList<>(listB);
listA.forEach(sm -> {
         notInA.remove(a.getServiceId());
         sm.setActive(listB.contains(sm.getServiceId()));
         listC.add(sm);
      });

notInA.forEach(id -> {
         ServiceMap newMap = new ServiceMap(id);
         newMap.setActive(true);
         listC.add(newMap);
      });
List notInA=newarraylist(listB);
listA.forEach(sm->{
notInA.remove(a.getServiceId());
setActive(listB.contains(sm.getServiceId());
添加(sm);
});
notInA.forEach(id->{
ServiceMap newMap=新ServiceMap(id);
newMap.setActive(true);
listC.add(newMap);
});

非常感谢,真的帮了我很多忙。一个问题:您提到了“listA.stream().peek(a->notInA.remove(a.getServiceId())”。你能帮我吗explain@Krish我删除了它,因为我编辑为根本不使用流。但作为一种解释,
peek
是指在您实际消费流之前能够访问流的每个元素,即有点像
forEach
,而流不会被终止。除了调试之外,使用它不是很好的风格,这也是我将答案直接更改为使用列表中的
forEach
的原因之一。感谢您的解释。我不熟悉java8 lambda表达式和流。
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 new list by validating records from both lists above
listA.stream().forEach(e -> {
    //noneMatch means we have to deactivate record in DB
    if (listB.parallelStream().noneMatch(x -> x == e.getServiceId())) {
        ServiceMap recordToDeactivate = e;
        e.setIsActive(false);
        listC.add(recordToDeactivate);
        return;
    }

    listB.stream().forEach(x -> {
        // if the record already added to listC then continue to next record
        if (listC.stream().anyMatch(e2->e2.getServiceId() == x)) {
            return;
        }
        //Matched means we have to activate record in DB
        if (x == e.getServiceId()) {
            ServiceMap recordToActivate = e;
            e.setIsActive(true);
            listC.add(recordToActivate);
            return;
        } else {
            //Not Matched means create new record in DB
            ServiceMap newM = new ServiceMap();
            newM.setIsActive(true);
            newM.setServiceId(x);
            listC.add(newM);
        }
    });

});
List<Integer> notInA = new ArrayList<>(listB);
listA.forEach(sm -> {
         notInA.remove(a.getServiceId());
         sm.setActive(listB.contains(sm.getServiceId()));
         listC.add(sm);
      });

notInA.forEach(id -> {
         ServiceMap newMap = new ServiceMap(id);
         newMap.setActive(true);
         listC.add(newMap);
      });