【发布时间】:2018-04-19 00:16:53
【问题描述】:
我的列表如下所示:
列表
[{
type: "1"
price: 10.0
count: 2
},
type: "1"
price: 15.0
count: 3
},
type: "2"
price: 20.0
count: 2
},
type: "2"
price: 30.0
count: 3
}]
我需要对它进行分组,以便我只有两种类型(在这种情况下)。
输出:
[{
type: "1"
price: 25.0
count: 5
},
type: "2"
price: 50.0
count: 5
}]
因此,基本上,列表中的元素数量将等于唯一类型的数量。价格和计数等其他值将被求和。
我试过用这个 -
report.stream().collect(Collectors.groupingBy(x -> x.getStuffe));
但是,这给出了一个地图。我不需要地图,我需要列表。
我该怎么做?
【问题讨论】: