I have list of objects List<BoM>
in BoM I have a List<BoMLine>
, now I have to sort BoM
list by one of BoMLine property using reduce and return a sorted List in a method,
public static List<BoM> sortBoms() {
List<BoM> sortedBomList = new ArrayList<>();
BoM bomn = new BoM();
sortedBomList.add(bomList.parallelStream().reduce(bomn,
(bom1, bom2) -> sortBoM(bom1, bom2)));
System.out.println(sortedBomList.size());
return sortedBomList;
}
bomList is List of BoM,and sortBoM method:
private static BoM sortBoM(BoM bom1, BoM bom2) {
bom2.getLine().stream()
.sorted((l1, l2) -> l1.getLineNum().compareTo(l2.getLineNum()));
bom1 = bom2;
return bom1;
}
BoM class:
public class BoM implements Domain {
private String BomCode;
private List<BoMLine> line = new ArrayList<BoMLine>();
public String getBomCode() {
return BomCode;
}
public void setBomCode(String bomCode) {
BomCode = bomCode;
}
public List<BoMLine> getLine() {
return line;
}
public void setLine(List<BoMLine> line) {
this.line = line;
}
public void addLine(BoMLine bomLine) {
bomLine.setbOM(this);
line.add(bomLine);
}}
and BoMLine class:
public class BoMLine implements Domain {
private Long lineNum;
private String material;
private BigDecimal Qty;
private BoM bOM;
public Long getLineNum() {
return lineNum;
}
public void setLineNum(Long lineNum) {
this.lineNum = lineNum;
}
public String getMaterial() {
return material;
}
public void setMaterial(String material) {
this.material = material;
}
public BigDecimal getQty() {
return Qty;
}
public void setQty(BigDecimal qty) {
Qty = qty;
}
public BoM getbOM() {
return bOM;
}
public void setbOM(BoM bOM) {
this.bOM = bOM;
}
public String getBoMCode() {
return bOM.getBomCode();
}
@Override
public String toString() {
return "BoMLine [ bOM=" + bOM.getBomCode() + ", lineNum=" + lineNum
+ ", material=" + material + ", Qty=" + Qty + "]";
}}
I have to order BoM list by BoMLine lineNum. but it just returns one object of bomList.Any help?