Java 8嵌套lambda函数查找特定键的总和

时间:2016-11-11 22:11:30

标签: java function lambda

我正在使用java 8并使用lambda函数。我有一个嵌套对象列表,每个对象main包含相同对象的列表,可以转到n级。只是想知道我们是否可以使用lambda函数或Stream API来获取特定属性的总和(sumofhead在下面),取决于对象中的一个特定键(如果操作是真的),然后遍历它的子节点(并找到sum),直到孩子们空。

以下是我收到的json

[{
    "mainheadname": "Assets",
    "sumofhead": 120.0000,
    "id": 102,
    "children": [{
        "mainheadname": "Cash",
        "sumofhead": 9900.0000,
        "id": 107,
        "children": [{
            "mainheadname": "Cash Counter1",
            "sumofhead": 17662.2100,
            "id": 153,
            "children": [{
                "mainheadname": "CashCounterChild",
                "sumofhead": 0.0,
                "id": 1179,
                "children": [{
                    "mainheadname": "Name New",
                    "sumofhead": -10.0000,
                    "id": 1242,
                    "children": [{
                        "mainheadname": "Name New Child",
                        "sumofhead": 0.0,
                        "id": 1247,
                        "children": [{
                            "mainheadname": "Nec Chil d",
                            "sumofhead": 0.0,
                            "id": 1248,
                            "children": [{
                                "mainheadname": "NEChild",
                                "sumofhead": 0.0,
                                "id": 1249,
                                "children": [],
                                "coacode": "CC12000",
                                "defuldledgertype": 1,
                                "current": null,
                                "operational": "True"
                            }],
                            "coacode": "rt120",
                            "defuldledgertype": 1,
                            "current": "False",
                            "operational": "True"
                        }],
                        "coacode": "dd22",
                        "defuldledgertype": 1,
                        "current": null,
                        "operational": "True"
                    }],
                    "coacode": "12000",
                    "defuldledgertype": 1,
                    "current": null,
                    "operational": null
                }, {
                    "mainheadname": "New 1",
                    "sumofhead": 0.0,
                    "id": 1246,
                    "children": [],
                    "coacode": "C678",
                    "defuldledgertype": 1,
                    "current": null,
                    "operational": "True"
                }],
                "coacode": "ChildCC",
                "defuldledgertype": 1,
                "current": null,
                "operational": "True"
            }, {
                "mainheadname": "CashCounterChild1",
                "sumofhead": 0.0,
                "id": 1245,
                "children": [],
                "coacode": "C120",
                "defuldledgertype": 1,
                "current": null,
                "operational": "True"
            }],
            "coacode": "CC122",
            "defuldledgertype": 1,
            "current": "True",
            "operational": null
        }, {
            "mainheadname": "Cash CounterOne",
            "sumofhead": 37211.6000,
            "id": 158,
            "children": [{
                "mainheadname": "New",
                "sumofhead": 0.0,
                "id": 1243,
                "children": [{
                    "mainheadname": "New Child Name",
                    "sumofhead": 0.0,
                    "id": 1244,
                    "children": [],
                    "coacode": "AC120",
                    "defuldledgertype": 2,
                    "current": null,
                    "operational": "True"
                }],
                "coacode": "c120",
                "defuldledgertype": 1,
                "current": "False",
                "operational": "True"
            }],
            "coacode": "CCCCC",
            "defuldledgertype": 1,
            "current": "True",
            "operational": null
        }, {
            "mainheadname": "Ne Test Cash Counter",
            "sumofhead": -1060.0000,
            "id": 1159,
            "children": [],
            "coacode": "1200",
            "defuldledgertype": 1,
            "current": null,
            "operational": "True"
        }, {
            "mainheadname": "New Counter test",
            "sumofhead": 0.0,
            "id": 1162,
            "children": [],
            "coacode": "CCode",
            "defuldledgertype": 1,
            "current": null,
            "operational": null
        }, {
            "mainheadname": "New Thing Test",
            "sumofhead": 10.0000,
            "id": 1163,
            "children": [],
            "coacode": "COde",
            "defuldledgertype": 1,
            "current": "False",
            "operational": "False"
        }, {
            "mainheadname": "Evening Counter",
            "sumofhead": 0.0,
            "id": 1176,
            "children": [],
            "coacode": "EC",
            "defuldledgertype": 1,
            "current": null,
            "operational": "True"
        }, {
            "mainheadname": "Morning Counter",
            "sumofhead": 0.0,
            "id": 1177,
            "children": [],
            "coacode": "zyz",
            "defuldledgertype": 1,
            "current": null,
            "operational": "True"
        }, {
            "mainheadname": "Cash Operations",
            "sumofhead": 0.0,
            "id": 1376,
            "children": [],
            "coacode": "120",
            "defuldledgertype": 1,
            "current": "True",
            "operational": null
        }],
        "coacode": "CA",
        "defuldledgertype": 1,
        "current": "true",
        "operational": null
    }, {
        "mainheadname": "Receivable",
        "sumofhead": 0.0,
        "id": 108,
        "children": [{
            "mainheadname": "Babar",
            "sumofhead": 5310.7200,
            "id": 154,
            "children": [],
            "coacode": "Babar cus",
            "defuldledgertype": 1,
            "current": "True",
            "operational": null
        }],
        "coacode": "RE",
        "defuldledgertype": 1,
        "current": "true",
        "operational": null
    }, {
        "mainheadname": "Inventory",
        "sumofhead": 107804.6000,
        "id": 110,
        "children": [],
        "coacode": "INV",
        "defuldledgertype": 1,
        "current": "true",
        "operational": null
    }, {
        "mainheadname": "Bank Transaction",
        "sumofhead": 0.0,
        "id": 111,
        "children": [{
            "mainheadname": "MCB bankacount",
            "sumofhead": -3071.8000,
            "id": 1253,
            "children": [],
            "coacode": "bmcb",
            "defuldledgertype": 1,
            "current": "True",
            "operational": null
        }],
        "coacode": "BAN",
        "defuldledgertype": 1,
        "current": "true",
        "operational": null
    }, {
        "mainheadname": "Accumulated Depreciation",
        "sumofhead": 0.0,
        "id": 119,
        "children": [],
        "coacode": "ACDEP",
        "defuldledgertype": 2,
        "current": "false",
        "operational": null
    }],
    "coacode": "A1",
    "defuldledgertype": 1,
    "current": null,
    "operational": null
}, {
    "mainheadname": "Liabilities",
    "sumofhead": 0.0,
    "id": 103,
    "children": [{
        "mainheadname": "Payable",
        "sumofhead": 0.0,
        "id": 109,
        "children": [{
            "mainheadname": "Employee Salary Account",
            "sumofhead": 0.0,
            "id": 113,
            "children": [{
                "mainheadname": "RehanAbbas",
                "sumofhead": 0.0,
                "id": 122,
                "children": [],
                "coacode": "Rehan emp",
                "defuldledgertype": 2,
                "current": "True",
                "operational": null
            }, {
                "mainheadname": "Ehtesham",
                "sumofhead": 0.0,
                "id": 1180,
                "children": [],
                "coacode": "Ehtesham emp",
                "defuldledgertype": 2,
                "current": "True",
                "operational": null
            }],
            "coacode": "EMP",
            "defuldledgertype": 2,
            "current": "true",
            "operational": null
        }, {
            "mainheadname": "Shoaib",
            "sumofhead": -22850.0000,
            "id": 124,
            "children": [],
            "coacode": "Shoaib Ven",
            "defuldledgertype": 2,
            "current": "True",
            "operational": null
        }, {
            "mainheadname": "Ahmad",
            "sumofhead": 7020.0000,
            "id": 1276,
            "children": [],
            "coacode": "Ahmad Ven",
            "defuldledgertype": 2,
            "current": "True",
            "operational": null
        }, {
            "mainheadname": "Abbasi",
            "sumofhead": 0.0,
            "id": 1383,
            "children": [],
            "coacode": "Abbasi Ven",
            "defuldledgertype": 2,
            "current": "True",
            "operational": null
        }, {
            "mainheadname": "Abbasi",
            "sumofhead": 26100.0000,
            "id": 1384,
            "children": [],
            "coacode": "Abbasi Ven",
            "defuldledgertype": 2,
            "current": "True",
            "operational": null
        }],
        "coacode": "Pay",
        "defuldledgertype": 2,
        "current": "true",
        "operational": null
    }, {
        "mainheadname": "Paid up Capital",
        "sumofhead": 0.0,
        "id": 112,
        "children": [],
        "coacode": "PCAP",
        "defuldledgertype": 2,
        "current": "false",
        "operational": null
    }],
    "coacode": "L1",
    "defuldledgertype": 2,
    "current": null,
    "operational": null
}, {
    "mainheadname": "Expense",
    "sumofhead": 0.0,
    "id": 104,
    "children": [{
        "mainheadname": "Discount given",
        "sumofhead": 3320.4000,
        "id": 115,
        "children": [],
        "coacode": "DISE",
        "defuldledgertype": 2,
        "current": null,
        "operational": "true"
    }, {
        "mainheadname": "Depreciation",
        "sumofhead": 0.0,
        "id": 118,
        "children": [{
            "mainheadname": "Depreciation Computer",
            "sumofhead": 0.0,
            "id": 1157,
            "children": [],
            "coacode": "DC",
            "defuldledgertype": 1,
            "current": null,
            "operational": null
        }],
        "coacode": "Depreciation",
        "defuldledgertype": 1,
        "current": null,
        "operational": "false"
    }, {
        "mainheadname": "Cost of Goods",
        "sumofhead": 143719.8600,
        "id": 120,
        "children": [],
        "coacode": "COG",
        "defuldledgertype": 1,
        "current": null,
        "operational": "true"
    }, {
        "mainheadname": "Services sold",
        "sumofhead": 0.0,
        "id": 121,
        "children": [],
        "coacode": "SS",
        "defuldledgertype": 1,
        "current": null,
        "operational": "true"
    }],
    "coacode": "EX1",
    "defuldledgertype": 1,
    "current": null,
    "operational": null
}, {
    "mainheadname": "Income",
    "sumofhead": 0.0,
    "id": 105,
    "children": [{
        "mainheadname": "discount received",
        "sumofhead": 0.0,
        "id": 114,
        "children": [],
        "coacode": "DISI",
        "defuldledgertype": 2,
        "current": null,
        "operational": "true"
    }, {
        "mainheadname": "Sale of Product",
        "sumofhead": -207183.1300,
        "id": 116,
        "children": [],
        "coacode": "IIS",
        "defuldledgertype": 1,
        "current": null,
        "operational": "true"
    }, {
        "mainheadname": "Income from Services",
        "sumofhead": 0.0,
        "id": 117,
        "children": [],
        "coacode": "IDS",
        "defuldledgertype": 2,
        "current": null,
        "operational": "true"
    }],
    "coacode": "INC",
    "defuldledgertype": 2,
    "current": null,
    "operational": null
}, {
    "mainheadname": "GST",
    "sumofhead": -139164.4600,
    "id": 106,
    "children": [],
    "coacode": "GST",
    "defuldledgertype": 0,
    "current": null,
    "operational": null
}]

我上面的Java对象如下:

package com.sabre.rest.models;

import java.util.List;

public class COALedga {

    private List<COALedga> children; 
    private String coacode; 
    private int defuldledgertype;
    private int id; 
    private String mainheadname; 
    private String operational;
    private int sumofhead;

    public COALedger() {
        // TODO Auto-generated constructor stub
    }

    public List<COALedga> getChildren() {
        return this.children;
    }

    public void setChildern(List<COALedga> children) {
        this.children = children;
    }

    public String getCoacode() {
        return coacode;
    }

    public void setCoacode(String coacode) {
        this.coacode = coacode;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String isOperational() {
        return operational;
    }

    public void setOperational(String operational) {
        this.operational = operational;
    }

    public int getDefuldledgertype() {
        return defuldledgertype;
    }

    public void setDefuldledgertype(int defuldledgertype) {
        this.defuldledgertype = defuldledgertype;
    }

    public String getMainHeadName() {
        return mainHeadName;
    }

    public void setMainHeadName(String mainHeadName) {
        this.mainHeadName = mainHeadName;
    }

    public int getSumOfHead() {
        return sumOfHead;
    }

    public void setSumOfHead(int sumOfHead) {
        this.sumOfHead = sumOfHead;
    }
}

1 个答案:

答案 0 :(得分:4)

不确定

int sum(COALedga c) {
   return c.getSumOfHead() + c.getChildren().stream().mapToInt(child -> sum(child)).sum();
}