当前位置:网站首页>开发中对集合里面的数据根据属性进行合并数量时犯的错误
开发中对集合里面的数据根据属性进行合并数量时犯的错误
2022-07-06 18:14:00 【小蚂蚁hjk】
1.先说下场景
小程序下单的时候对商品进行库存校验,由于前端传过来的商品集合并没有把相同sku的商品数量进行相加,所以再我做库存校验的时候过滤出需要的商品集合后进行了合并数据,导致在保存商品信息的时候多一条 合并商品数量数据
之前是这样写的
//过滤出符合条件的商品数据 List<OrderGoods> goods = goodsList.stream().filter(s -> !GoodsTypeEnum.GOODS_TYPE_02.getCode().equals(s.getGoodsType()) && !GoodsTypeEnum.GOODS_TYPE_04.getCode().equals(s.getGoodsType())).collect(Collectors.toList());
//集合数据合并
List<OrderGoods> orderGood = new ArrayList<>(goods .stream().collect(Collectors.toMap(OrderGoods::getSku, a -> a, (o1, o2) -> {
o1.setQuantity(String.valueOf(Math.abs(Integer.parseInt(o1.getQuantity())) + Math.abs(Integer.parseInt(o2.getQuantity()))));
return o1;
})).values());
发现这种合并会对原来的数据对象产生影响
所以就重新创建一个集合
//合并商品数量
List<OrderGoods> orderGoodsList = new ArrayList<>();
for (OrderGoods good : goods) {
OrderGoods orderGood = new OrderGoods();
orderGood.setSku(good.getSku());
orderGood.setQuantity(good.getQuantity());
orderGoodsList.add(orderGood);
}
//集合数据合并
List<OrderGoods> orderGood = new ArrayList<>(orderGoodsList.stream().collect(Collectors.toMap(OrderGoods::getSku, a -> a, (o1, o2) -> {
o1.setQuantity(String.valueOf(Math.abs(Integer.parseInt(o1.getQuantity())) + Math.abs(Integer.parseInt(o2.getQuantity()))));
return o1;
})).values());这样就不对原对象进行操作了,所以问题就解决了
边栏推荐
- 一起看看matlab工具箱内部是如何实现BP神经网络的
- 场景实践:基于函数计算快速搭建Wordpress博客系统
- Modify the system time of Px4 flight control
- Appium foundation - appium inspector positioning tool (I)
- Compile command line terminal swift
- Share a general compilation method of so dynamic library
- AcWing 1142. Busy urban problem solving (minimum spanning tree)
- 【C语言进阶篇】指针的8道笔试题
- Let's see how to realize BP neural network in Matlab toolbox
- Add PDF Title floating window
猜你喜欢

Can't you understand the code of linked list in C language? An article allows you to grasp the secondary pointer and deeply understand the various forms of parameter passing in the function parameter

JVM memory model

1123. The nearest common ancestor of the deepest leaf node

永久的摇篮

AcWing 1148. 秘密的牛奶运输 题解(最小生成树)

Appium自动化测试基础 — uiautomatorviewer定位工具

一文带你走进【内存泄漏】

百度飞将BMN时序动作定位框架 | 数据准备与训练指南 (下)

New job insights ~ leave the old and welcome the new~

Yunna | work order management measures, how to carry out work order management
随机推荐
盒子拉伸拉扯(左右模式)
搭建【Redis in CentOS7.x】
糊涂工具类(hutool)post请求设置body参数为json数据
Reptile practice (VI): novel of climbing pen interesting Pavilion
Yunna | work order management measures, how to carry out work order management
刨析《C语言》【进阶】付费知识【完结】
增加 pdf 标题浮窗
POJ 3177 redundant paths POJ 3352 road construction (dual connection)
C language instance_ two
图片打水印 缩放 和一个输入流的转换
JVM 内存模型
Make DIY welding smoke extractor with lighting
AcWing 345. 牛站 题解(floyd的性质、倍增)
golang 基础 —— 数据类型
ZOJ Problem Set – 2563 Long Dominoes 【如压力dp】
字符串转成日期对象
js如何快速创建一个长度为 n 的数组
Drag to change order
Compile command line terminal swift
刨析《C语言》【进阶】付费知识【二】