在确定正确的流式传输和收集数据方式时遇到问题。
以下是流
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
}
]
}
]
我需要如何调整自己的视频流才能获得高于预期的结果?
答案 0 :(得分:1)
通过@ernest_k解决了它-在flatMap
上使用.values()
public List<CalculatedItemCostPerStore> getCalcualtedItemCostPerStore() {
return 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()
.flatMap(Collection::stream).collect(Collectors.toList())
;
}