2016-11-11 8 views
1

私はJava 8を使用しており、ラムダ関数を扱っています。私はネストされたオブジェクトのリストを持っていて、各オブジェクトmainは同じオブジェクトのリストを含んでおり、nレベルに行くことができます。ラムダ関数またはストリームAPIを使用して、オブジェクト内の特定のキー(操作可能な場合)に応じて特定のプロパティ(次の場合はsumofhead)の合計を取ってから、子がループするまでヌル。Java 8のネストされたラムダ関数特定のキーの合計を見つける

以下のように私は上記のために持っている私が受け取った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; 
    } 
} 
+0

どのように深くネストされたこの構造は、することができますか?それは固定ですか? –

+1

jsonパスを使用して、必要なデータを取得して合計するのはなぜですか? –

+0

@BartoszBilicki @レベルまで行くことができます:)返信ありがとう – Sajid

答えて

4

確か:

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

おかげで、私のために働いた – Sajid

関連する問題