How to use reduce to sort lists in Java 8?

I have an object list list < BOM > in BOM, I have a list < bomline >, and now I have to use reduce pair Bo@R_403_2177 @Property to sort the BOM list and return a sorted list in the 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 the list of BOM. 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 course:

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 classes:

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 the BOM list through bomline linenum But it only returns Bomlist An object of any help?

Solution

You can use comparator Comparing creates a custom comparator and sorts each bomline in ascending order of line number:

List<BoM> sortedBomList = new ArrayList<>();
sortedBomList.forEach(bom -> bom.getLine().sort(comparing(BoMLine::getLineNum)));

Please note that this will change list < bomline > and list < BOM >, which may not be a good idea

A better way is to get invariance and create a constructor to get BOM code and BOM line list:

List<BoM> sortedBomList = 
        bomList.stream()
               .map(bom -> new BoM(
                               bom.BomCode,bom.getLine().stream()
                                            .sorted(comparing(BoMLine::getLineNum))
                                            .collect(toList())
                           )
               )
               .collect(toList());
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>