当前位置:网站首页>开发中对集合里面的数据根据属性进行合并数量时犯的错误
开发中对集合里面的数据根据属性进行合并数量时犯的错误
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());
这样就不对原对象进行操作了,所以问题就解决了
边栏推荐
- Long press the button to execute the function
- Comparison of picture beds of free white whoring
- AcWing 1141. LAN problem solving (kruskalkruskal finding the minimum spanning tree)
- AcWing 344. Solution to the problem of sightseeing tour (Floyd finding the minimum ring of undirected graph)
- json学习初体验–第三者jar包实现bean、List、map创json格式
- JS ES5也可以创建常量?
- AcWing 361. Sightseeing cow problem solution (SPFA seeking positive ring)
- Basic introduction and use of dvajs
- Domestic images of various languages, software and systems. It is enough to collect this warehouse: Thanks mirror
- 刨析《C语言》【进阶】付费知识【一】
猜你喜欢
Mongodb checks whether the table is imported successfully
C语言关于链表的代码看不懂?一篇文章让你拿捏二级指针并深入理解函数参数列表中传参的多种形式
设置Wordpress伪静态连接(无宝塔)
Yunna - work order management system and process, work order management specification
Gin 入门实战
爬虫实战(六):爬笔趣阁小说
刨析《C语言》【进阶】付费知识【一】
1123. The nearest common ancestor of the deepest leaf node
制作带照明的DIY焊接排烟器
AcWing 345. Cattle station solution (nature and multiplication of Floyd)
随机推荐
Long press the button to execute the function
Yunna | work order management software, work order management software app
我如何编码8个小时而不会感到疲倦。
golang 基础 —— 数据类型
百度飞将BMN时序动作定位框架 | 数据准备与训练指南 (上)
grep查找进程时,忽略grep进程本身
uva 1401 dp+Trie
Yunna - work order management system and process, work order management specification
今日问题-2022/7/4 lambda体中修改String引用类型变量
AcWing 1140. Shortest network (minimum spanning tree)
LeetCode:1175. Prime permutation
Use nodejs to determine which projects are packaged + released
刨析《C语言》【进阶】付费知识【二】
Sword finger offer II 035 Minimum time difference - quick sort plus data conversion
Reptile practice (VI): novel of climbing pen interesting Pavilion
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
Basic introduction and use of dvajs
增加 pdf 标题浮窗
黑马笔记---创建不可变集合与Stream流
Blue Bridge Cup 2022 13th provincial competition real topic - block painting