当前位置:网站首页>开发中对集合里面的数据根据属性进行合并数量时犯的错误
开发中对集合里面的数据根据属性进行合并数量时犯的错误
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());
这样就不对原对象进行操作了,所以问题就解决了
边栏推荐
- Sword finger offer II 035 Minimum time difference - quick sort plus data conversion
- Blue Bridge Cup 2022 13th provincial competition real topic - block painting
- Appium自动化测试基础 — uiautomatorviewer定位工具
- 刨析《C语言》【进阶】付费知识【完结】
- 糊涂工具类(hutool)post请求设置body参数为json数据
- hdu 4661 Message Passing(木DP&amp;组合数学)
- Hutool post requests to set the body parameter to JSON data
- uva 1401 dp+Trie
- C语言实例_4
- tansig和logsig的差异,为什么BP喜欢用tansig
猜你喜欢
Dark horse notes - exception handling
Appium基础 — Appium Inspector定位工具(一)
场景实践:基于函数计算快速搭建Wordpress博客系统
454-百度面经1
New job insights ~ leave the old and welcome the new~
Yunna | work order management measures, how to carry out work order management
AcWing 361. Sightseeing cow problem solution (SPFA seeking positive ring)
新工作感悟~辞旧迎新~
Comparison of picture beds of free white whoring
我如何编码8个小时而不会感到疲倦。
随机推荐
AcWing 346. Solution to the problem of water splashing festival in the corridor (deduction formula, minimum spanning tree)
Blue Bridge Cup 2022 13th provincial competition real topic - block painting
C语言实例_3
JVM memory model
糊涂工具类(hutool)post请求设置body参数为json数据
从底层结构开始学习FPGA----FIFO IP的定制与测试
JS how to quickly create an array with length n
搭建【Redis in CentOS7.x】
Use nodejs to determine which projects are packaged + released
Public key \ private SSH avoid password login
AcWing 1148. 秘密的牛奶运输 题解(最小生成树)
Transplant DAC chip mcp4725 to nuc980
JS ES5也可以創建常量?
AcWing 904. 虫洞 题解(spfa求负环)
Gin 入门实战
黑马笔记---异常处理
新工作感悟~辞旧迎新~
Google发布安全更新,修复Chrome中已被利用的0 day
Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP
AcWing 346. 走廊泼水节 题解(推公式、最小生成树)