Groovy列表:按元素列表分组

时间:2019-11-27 12:29:10

标签: dictionary groovy collections

我基本上想对地图进行分组,我必须按汽车和颜色分组并对其价格求和,如果任何一组价格大于25,则按汽车,颜色和电机分组结果并乘以总和* 3,部分已经完成,但是我被第二组困住了:

def row1 = ["car":'A',"color":'B',"motor":'C', "price": 12]
def row2 = ["car":'A',"color":'B',"motor":'C', "price": 12]
def row3 = ["car":'A',"color":'B',"motor":'D', "price": 2]
def row4 = ["car":'B',"color":'B',"motor":'D', "price": 13]
def arrayRows = []
arrayRows.add(row1)
arrayRows.add(row2)
arrayRows.add(row3)
arrayRows.add(row4)
println(arrayRows) //[[car:A, color:B, motor:C, price:12], [car:A, color:B, motor:C, price:12], [car:A, color:B, motor:D, price:2], [car:B, color:B, motor:D, price:13]]
def groups = arrayRows.groupBy {row -> [Car:row.car, Color:row.color] }.collect{k , v ->
    [
        car:k.Car,
        color:k.Color,
        motor:v.motor,
        price:v.collect { it.price }.sum()
    ]
}
println(groups) //[[car:A, color:B, motor:[C, C, D], price:26], [car:B, color:B, motor:[D], price:13]]
for(group in groups){
    if (group.price > 25){ //26>25
        println group //[car:A, color:B, motor:[C, C, D], price:26]
        //def groupByCarColorMotor = arrayRows.groupBy {[Car: group.car, Color: group.color, Motor:?]} | Must group by car, color and motor, and multiply * 3 it's price but since motor is an array, I'm not sure how to do so, I've tried groupBy { row -> group.it.motor} etc
    }
}

如果有以下情况,我应该如何第二次分组: [汽车:A,颜色:B,电机:[C,C,D] 我应该按以下条件分组: [汽车:A,颜色:B,马达:C] 和 [汽车:A,颜色:B,电动机:D]

预期输出应为: [[[“ car”:'A',“ color”:'B',“ motor”:'C',“ price”:72,[“” car“:'A',” color“:'B',”电机”:“ D”,“价格”:6]]

编辑; 我几乎设法做到了,问题是我得到了一组地图阵列,你也可能会得到它的支持。

def arrayRows = [ 
        ["car":'A',"color":'B',"motor":'C', "price": 12],
        ["car":'A',"color":'B',"motor":'C', "price": 12],
        ["car":'A',"color":'B',"motor":'D', "price": 2],
        ["car":'B',"color":'B',"motor":'D', "price": 13]
        ]

println(arrayRows) //[[car:A, color:B, motor:C, price:12], [car:A, color:B, motor:C, price:12], [car:A, color:B, motor:D, price:2], [car:B, color:B, motor:D, price:13]]
def groups = arrayRows.groupBy {row -> [Car:row.car, Color:row.color] }.collect{k , v ->
    [
            car:k.Car,
            color:k.Color,
            price:v.collect { it.price }.sum()
    ]
}.findAll{it.price > 25}

def groupByCarColor = []
for (group in groups){
    groupByCarColor.add(arrayRows.findAll{ row -> row.car == group.car && row.color == group.color}.groupBy {row -> [Car:group.car, Color:group.color, Motor:row.motor] }.collect{k , v ->
        [
                car:k.Car,
                color:k.Color,
                motor:k.Motor,
                price:v.collect { it.price }.sum()*3
        ]
    })
}

输出:[[[汽车:A,颜色:B,马达:C,价格:72],[汽车:A,颜色:B,马达:D,价格:6]]]

2 个答案:

答案 0 :(得分:1)

直接完成您的家庭作业:

constructSearch(events)

答案 1 :(得分:1)

您的目标是通过三元组找到组-但您想过滤掉谓词在子组上失败的那些组。例如

def arrayRows = [ 
    [car:'A', motor:'C', color:'B',price: 12],
    [car:'A', motor:'C', color:'B',price: 12],
    [car:'A', motor:'D', color:'B',price:  2],
    [car:'B', motor:'D', color:'B',price: 13],
]


def output = arrayRows.inject([:].withDefault{ 0 }){ acc, row -> // group by the triplet and sum up the price
    acc[row.subMap(['car','motor','color'])]+=row.price; acc
}.groupBy{ k, _ -> // group by the filter criteria tuple
    k.subMap(['car','color'])
}.findAll{ _, v -> // eliminate the tuple-groups where price is to low
    v.values().sum() > 25
}.collectMany{ _, vs -> // reshape the data
    vs.collect{ k, v ->
        k + [price: v * 3]
    }
}

assert output==[[car:"A", motor:"C", color:"B", price:72], [car:"A", motor:"D", color:"B", price:6]]