当前位置:网站首页>开发中对集合里面的数据根据属性进行合并数量时犯的错误
开发中对集合里面的数据根据属性进行合并数量时犯的错误
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());这样就不对原对象进行操作了,所以问题就解决了
边栏推荐
- 初识MySQL
- AcWing 1141. LAN problem solving (kruskalkruskal finding the minimum spanning tree)
- Telnet,SSH1,SSH2,Telnet/SSL,Rlogin,Serial,TAPI,RAW
- Long press the button to execute the function
- 刨析《C语言》【进阶】付费知识【一】
- Amway wave C2 tools
- AcWing 1140. 最短网络 (最小生成树)
- C语言【23道】经典面试题【下】
- The difference between Tansig and logsig. Why does BP like to use Tansig
- LeetCode. 剑指offer 62. 圆圈中最后剩下的数
猜你喜欢

Blue Bridge Cup 2022 13th provincial competition real topic - block painting

AcWing 361. Sightseeing cow problem solution (SPFA seeking positive ring)

Baidu flying general BMN timing action positioning framework | data preparation and training guide (Part 2)

Set WordPress pseudo static connection (no pagoda)

新工作感悟~辞旧迎新~

Today's question -2022/7/4 modify string reference type variables in lambda body

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

Start from the bottom structure to learn the customization and testing of fpga---- FIFO IP

Basic introduction and use of dvajs

LeetCode:1175. Prime permutation
随机推荐
How to prevent overfitting in cross validation
json学习初体验–第三者jar包实现bean、List、map创json格式
AcWing 1141. LAN problem solving (kruskalkruskal finding the minimum spanning tree)
736. LISP syntax parsing: DFS simulation questions
Let's see how to realize BP neural network in Matlab toolbox
刨析《C语言》【进阶】付费知识【完结】
swiper组件中使用video导致全屏错位
AcWing 344. 观光之旅题解(floyd求无向图的最小环问题)
Appium foundation - appium inspector positioning tool (I)
C语言实例_5
AcWing 1148. Secret milk transportation problem solution (minimum spanning tree)
Right mouse button customization
Telnet,SSH1,SSH2,Telnet/SSL,Rlogin,Serial,TAPI,RAW
Dark horse notes - exception handling
ZOJ problem set – 2563 long dominoes [e.g. pressure DP]
AcWing 361. Sightseeing cow problem solution (SPFA seeking positive ring)
tansig和logsig的差异,为什么BP喜欢用tansig
场景实践:基于函数计算快速搭建Wordpress博客系统
ZOJ Problem Set – 2563 Long Dominoes 【如压力dp】
JS ES5也可以創建常量?