流如下所示
public List<List<CalculatedItemCostPerStore>> getCalcualtedItemCostPerStore() {
var it = item
.getQuantities().stream().map(quantities -> {
Store.StoreCosts<CostType, BigDecimal> storeCosts = quantities.getStore().getStoreCosts();
return new CalculatedItemCostPerStore(quantities.getStore().getName(), new TreeSet<>(quantities
.getItemCostEvents()
.stream()
.map(e -> new CalculatedItemCost(e.getCostTrigger(), e.getTriggerQuantity().multiply(storeCosts.get(e.getCostTrigger()))))
.collect(
Collectors.toMap(CalculatedItemCost::getCostType, Function.identity(), (a, b) -> new CalculatedItemCost(a.getCostType(), a.getCost().add(b.getCost())))
).values()));
}).collect(Collectors.groupingBy(CalculatedItemCostPerStore::getStore)).values().stream().collect(Collectors.toList());
return it;
}
被流到
@Data
@AllArgsConstructor
static class CalculatedItemCost implements Comparable<CalculatedItemCost> {
private CostType costType;
private BigDecimal cost;
@Override
public int compareTo(CalculatedItemCost o) {
return this.costType.toString().compareTo(o.getCostType().toString());
}
}
@Data
@AllArgsConstructor
static class CalculatedItemCostPerStore {
private String store;
private TreeSet<CalculatedItemCost> calculatedItemCosts;
}
itemCostEvents是
{
"createdAt": "2018-08-29T00:00:00",
"costTrigger": "STORAGE_COST_PER_CBM_PER_DAY",
"triggerQuantity": 33
}
上面的流创建一个由下面的json表示的结果。正如你所看到的,这是一个
List<List<CalculatedItemCostPerStore>>
"calcualtedItemCostPerStore": [
[
{
"store": "foo",
"calculatedItemCosts": [
{
"costType": "ADDITIONAL_COST_OFFICE_PER_HOUR",
"cost": 1368
},
{
"costType": "STORAGE_COST_PER_CBM_PER_DAY",
"cost": 287.1
}
]
}
],
[
{
"store": "bar",
"calculatedItemCosts": [
{
"costType": "ADDITIONAL_COST_OFFICE_PER_HOUR",
"cost": 38
}
]
}
]
]
数量可以有任意数量的出现,其中每个都可以有一个存储。
我想要的是一张单人票
List<CalculatedItemCostPerShop>
如下图所示
"calcualtedItemCostPerStore": [
{
"store": "foo",
"calculatedItemCosts": [
{
"costType": "ADDITIONAL_COST_OFFICE_PER_HOUR",
"cost": 1368
},
{
"costType": "STORAGE_COST_PER_CBM_PER_DAY",
"cost": 287.1
}
]
},
{
"store": "bar",
"calculatedItemCosts": [
{
"costType": "ADDITIONAL_COST_OFFICE_PER_HOUR",
"cost": 38
}
]
}
]
我需要如何调整我的流以获得上述结果?