1.stream简化双重for循环匹配
简化前
public List<Z1> buildZ1(List<X1> xlist,List<Y1> ylist){
List<Z1> zList=new ArrayList();
for(X1 x1:xlist){
for(Y1 y1:ylist){
Z1 z1=new Z1();
z1.setXXX(x1.getXxx())
if(y1.getAct().equals(x1.getAct())){
z1.setxxx(y1.getXxx());
zList.add(z1);
continue;
}
zList.add(z1);
}
}
return zList;
}
简化后
public List<Z1> buildZ1(List<X1> xlist,List<Y1> ylist){
List<Z1> zList=new ArrayList();
for(X1 x1:xlist){
Z1 z1=new Z1();
z1.setXXX(x1.getXxx())
//获取y1中某某属性与x1中的某个属性相同的。
Optional<Y1> any=y1.stream.filter(v->Objects.equals(v.getAct(),x1.getAct())).findAny();
if(any.isPresent()){
z1.setxxx(any.get().getXxx());
zList.add(z1);
continue;
}
zList.add(z1);
}
return zList;
}
这样就少了一层for循环,看起来更加清爽