用Java8中的另一个列表过滤对象列表
我是Java8新手,尝试将一个列表与另一个列表进行比较,但无法进行过滤用Java8中的另一个列表过滤对象列表,java,list,collections,filter,java-stream,Java,List,Collections,Filter,Java Stream,我是Java8新手,尝试将一个列表与另一个列表进行比较,但无法进行过滤 ProductDetail.class public class ProductDetail { long productNo; String productStr; long productCount; List<SerialCode> serialCodes; } SerialCode.class public class SerialCode { int seri
ProductDetail.class
public class ProductDetail {
long productNo;
String productStr;
long productCount;
List<SerialCode> serialCodes;
}
SerialCode.class
public class SerialCode {
int serialId;
String serialName;
}
public class Main {
public Main() {
// TODO Auto-generated constructor stub
List<ProductDetail> pdList = new ArrayList();
List<SerialCode> sdList = new ArrayList();
SerialCode sd = new SerialCode();
sd.setSerialName("sname1");
sdList.add(sd);
List<SerialCode> sdList1 = new ArrayList();
SerialCode sd1 = new SerialCode();
sd1.setSerialId(100013);
sd1.setSerialName("sname2");
sdList1.add(sd1);
ProductDetail pd1 = new ProductDetail();
pd1.setProductNo(1234);
pd1.setProductStr("STR1");
pd1.setProductCount(20);
pd1.setSerialCodes(sdList);
ProductDetail pd2 = new ProductDetail();
pd2.setProductNo(1255);
pd2.setProductStr("STR2");
pd2.setProductCount(40);
pd2.setSerialCodes(sdList1);
pdList.add(pd1);
pdList.add(pd2);
}
}
ProductDetail.class
公共类产品详细信息{
长产品号;
字符串productStr;
长产品计数;
列出序列号;
}
SerialCode.class
公共类序列码{
int-serialId;
字符串序列名;
}
公共班机{
公用干管(){
//TODO自动生成的构造函数存根
List pdList=new ArrayList();
List sdList=new ArrayList();
SerialCode sd=新的SerialCode();
sd.setSerialName(“sname1”);
sdList.add(sd);
List sdList1=new ArrayList();
SerialCode sd1=新的SerialCode();
sd1.SetSerialide(100013);
sd1.设置序列名称(“sname2”);
sdList1.add(sd1);
ProductDetail pd1=新的ProductDetail();
pd1.设置产品编号(1234);
pd1.setProductStr(“STR1”);
pd1.setProductCount(20);
pd1.设置串行代码(sdList);
ProductDetail pd2=新的ProductDetail();
pd2.setProductNo(1255);
pd2.setProductStr(“STR2”);
pd2.setProductCount(40);
pd2.设置串行代码(sdList1);
pdList.add(pd1);
pdList.add(pd2);
}
}
要与之比较的子列表:
List<BigDecimal> sidList = new ArrayList();
sidList.add(100012);
List sidList=new ArrayList();
sidList.add(100012);
因此,在这种情况下,结果应该只返回
列表
其中包含pd1对象
到目前为止,我已经这样做了,但它不工作
List<ProductDetail> listOutput =
pdList.stream()
.filter(e -> sidList.contains(e.getSerialCodes().stream().filter(f->f.getSerialId())))
.collect(Collectors.toList());
列表列表输出=
pdList.stream()
.filter(e->sidList.contains(e.getSerialCodes().stream().filter(f->f.getSerialId()))
.collect(Collectors.toList());
您可以这样做
List<ProductDetail> listOutput = new ArrayList<>();
pdList.forEach(e -> {
e.serialcodes.forEach(f -> {
if (sidList.contains(BigDecimal.valueOf(f.getSerialId()))) {
listOutput.add(e);
}
});
});
List listOutput=new ArrayList();
pdList.forEach(e->{
e、 serialcodes.forEach(f->{
if(sidList.contains(BigDecimal.valueOf(f.getSerialId())){
添加(e);
}
});
});
流操作不应修改或改变任何外部状态。因此,我建议使用以下方法:
final List<ProductDetail> list = pdList.stream()
.filter(p -> p.serialCodes.stream()
.map(s -> BigDecimal.valueOf(s.serialId))
.anyMatch(x -> sidList.stream().anyMatch(b -> b.equals(x))))
.collect(Collectors.toList());
final List=pdList.stream()
.filter(p->p.serialCodes.stream()
.map->BigDecimal.valueOf(s.serialId))
.anyMatch(x->sidList.stream().anyMatch(b->b.equals(x)))
.collect(Collectors.toList());
您能解释一下为什么结果列表中应该只有pd1
作为对象吗?为什么在这里使用BigDecimal
?根本不需要,对于这个-->sidList.add(100012)要编译,您需要执行sidList.add(BigDecimal.valueOf(100012))代码>或sidList.add(新的BigDecimal(100012))代码>。无论如何,在我看来,没有必要使用BigDecimal
。不要使用原始ArrayList,即所有新ArrayList()
应更改为新ArrayList()代码>。