Groovy web console

subscribe to the feed Subscribe
to this
site

Group By With 2 fields

Published 3 months ago by Tusar with tags GroupBy
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
def oltcList = [
    [
        id : 1,
        department : "Dept1",
        offerDetailsId : 11,
        tc : "Dept1-Tc1"
    ],
    [
        id : 2,
        department : "Dept1",
        offerDetailsId : 11,
        tc : "Dept1-Tc2"
    ],
    [
        id : 3,
        department : "Dept2",
        offerDetailsId : 11,
        tc : "Dept2-Tc3"
    ]
]

def odDeptMap = oltcList.groupBy({it.offerDetailsId}, {it.department})

            println("odDeptMap ------------------> " + odDeptMap)
            for(odDeptMapEntry in odDeptMap) {
                def offerDetailsId = odDeptMapEntry.key
                def deptOltcMap = odDeptMapEntry.value
                println("offerDetailsId --------------------------------------------------> " + offerDetailsId )
                for(deptOltcMapEntry in deptOltcMap) {
                    def department = deptOltcMapEntry.key
                    def oltcListByDept = deptOltcMapEntry.value
                    def adhocList = oltcListByDept ? oltcListByDept .groupBy({ it -> it?.offerDetailsId}) : null
                    println("department ----> " + department)
                    println("oltcListByDept ------------------> " + oltcListByDept )
                    println("adhocList -----------------------> " + adhocList )
           
                }
            }