动态金额 jsonArray 计算
数据格式 [{"name": "福利卡券", "type": "WELFARE_CARD_COUPON", "useNum": 0.01}, {"name": "现金", "type": "CASH", "useNum": 12.26}]
相加
/**
* costinfo 运算相加
*
* @param totalCostInfo
* @param costInfo
* @return
*/
private List<CostInfo> addCostInfo(List<CostInfo> totalCostInfo, List<CostInfo> costInfo) {
if (CollectionUtils.isEmpty(costInfo)) {
return totalCostInfo;
}
Map<String, CostInfo> costInfoTOMap = totalCostInfo
.stream()
.collect(Collectors.toMap(e -> e.getType(), t -> t));
Map<String, CostInfo> costInfoMap = costInfo
.stream()
.collect(Collectors.toMap(e -> e.getType(), t -> t));
Map<String, CostInfo> newMap = new HashMap<>();
for (Map.Entry<String, CostInfo> entry : costInfoTOMap.entrySet()) {
String key = entry.getKey();
CostInfo existCostInfo = costInfoMap.get(key);
// 存在
CostInfo newCostInfo = new CostInfo();
CostInfo value = entry.getValue();
if (null != existCostInfo) {
newCostInfo.setName(value.getName());
newCostInfo.setType(key);
newCostInfo.setUseNum(NumberUtil.add(value.getUseNum(), existCostInfo.getUseNum()));
} else {
newCostInfo.setName(value.getName());
newCostInfo.setType(key);
newCostInfo.setUseNum(value.getUseNum());
}
newMap.put(key, newCostInfo);
}
for (Map.Entry<String, CostInfo> entry : costInfoMap.entrySet()) {
String key = entry.getKey();
CostInfo value = entry.getValue();
CostInfo info = newMap.get(key);
if (null == info) {
CostInfo newCostInfo = new CostInfo();
newCostInfo.setName(value.getName());
newCostInfo.setType(key);
newCostInfo.setUseNum(value.getUseNum());
newMap.put(key, newCostInfo);
}
}
List<CostInfo> costInfoTOList = new ArrayList<>();
// 保证顺序
totalCostInfo.forEach(e -> {
CostInfo info = newMap.get(e.getType());
if (null == info) {
costInfoTOList.add(e);
} else {
costInfoTOList.add(info);
}
});
Map<String, CostInfo> costInfoTOListMap = costInfoTOList
.stream()
.collect(Collectors.toMap(e -> e.getType(), t -> t));
newMap.forEach((key, value) -> {
CostInfo info = costInfoTOListMap.get(key);
// 补全之前 没有加入的
if (null == info) {
costInfoTOList.add(value);
}
});
return costInfoTOList;
}
相减
/**
* costinfo 运算相减
*
* @param totalCostInfo 总的
* @param costInfo 被减数
* @return
*/
private List<CostInfo> subCostInfo(List<CostInfo> totalCostInfo, List<CostInfo> costInfo) {
if (CollectionUtils.isEmpty(totalCostInfo) || CollectionUtils.isEmpty(costInfo)) {
return totalCostInfo;
}
Map<String, CostInfo> costInfoTOMap = totalCostInfo
.stream()
.collect(Collectors.toMap(e -> e.getType(), t -> t));
Map<String, CostInfo> costInfoMap = costInfo
.stream()
.collect(Collectors.toMap(e -> e.getType(), t -> t));
Map<String, CostInfo> newMap = new HashMap<>();
for (Map.Entry<String, CostInfo> entry : costInfoTOMap.entrySet()) {
String key = entry.getKey();
CostInfo existCostInfo = costInfoMap.get(key);
// 存在
if (null != existCostInfo) {
CostInfo value = entry.getValue();
CostInfo newCostInfo = new CostInfo();
newCostInfo.setName(value.getName());
newCostInfo.setType(key);
newCostInfo.setUseNum(NumberUtil.sub(value.getUseNum(), existCostInfo.getUseNum()));
newMap.put(key, newCostInfo);
}
}
List<CostInfo> costInfoTOList = new ArrayList<>();
// 保证顺序
totalCostInfo.forEach(e -> {
CostInfo info = newMap.get(e.getType());
if (null == info) {
costInfoTOList.add(e);
} else {
costInfoTOList.add(info);
}
});
return costInfoTOList;
}
标签:info,newCostInfo,jsonArray,金额,value,CostInfo,totalCostInfo,key,动态 From: https://www.cnblogs.com/lyc88/p/17793936.html总结 : 一giao 我滴 giao ,这种格式的数据计算 有点小麻烦