我正在编写一段代码,循环遍历多个列表,并使用Java8 lambda表达式合并来自两个列表的唯一值,从而创建另一个列表。
模型类:
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));
使用java lambda表达式编写代码是否有效?
https://stackoverflow.com/questions/52505591
复制相似问题