当前位置:网站首页>Errors made in the development of merging the quantity of data in the set according to attributes
Errors made in the development of merging the quantity of data in the set according to attributes
2022-07-07 01:51:00 【Ant HJK】
1. Let's talk about the scene first
When the applet orders, it verifies the inventory of goods , Because the collection of products transmitted from the front end is not the same sku Add the quantity of goods , So when I do inventory verification, I filter out the required product set and consolidate the data , As a result, there is one more message when saving the product information Consolidate commodity quantity data
It was written like this before
// Filter out qualified product data 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());
// Consolidation of set data 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());
It is found that this merging will affect the original data objects
So recreate a collection
// Consolidated product quantity List<OrderGoods> orderGoodsList = new ArrayList<>(); for (OrderGoods good : goods) { OrderGoods orderGood = new OrderGoods(); orderGood.setSku(good.getSku()); orderGood.setQuantity(good.getQuantity()); orderGoodsList.add(orderGood); } // Consolidation of set data 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());
In this way, the original object will not be operated , So the problem was solved
边栏推荐
- AcWing 361. 观光奶牛 题解(spfa求正环)
- 【唯一】的“万字配图“ | 讲透【链式存储结构】是什么?
- 公钥\私人 ssh避password登陆
- 454-百度面经1
- 场景实践:基于函数计算快速搭建Wordpress博客系统
- Mongodb checks whether the table is imported successfully
- C语言【23道】经典面试题【下】
- Related programming problems of string
- AcWing 361. Sightseeing cow problem solution (SPFA seeking positive ring)
- json学习初体验–第三者jar包实现bean、List、map创json格式
猜你喜欢
随机推荐
增加 pdf 标题浮窗
ROS学习(十九)机器人SLAM功能包——cartographer
C language instance_ five
The cradle of eternity
mongodb查看表是否导入成功
736. LISP syntax parsing: DFS simulation questions
sql中批量删除数据---实体中的集合
搭建【Redis in CentOS7.x】
Shortcut keys commonly used in idea
蓝桥杯2022年第十三届省赛真题-积木画
json学习初体验–第三者jar包实现bean、List、map创json格式
Yiwen takes you into [memory leak]
New job insights ~ leave the old and welcome the new~
ROS学习(21)机器人SLAM功能包——orbslam的安装与测试
JS ES5也可以創建常量?
鼠标右键 自定义
Gin introduction practice
Add PDF Title floating window
HDU 4661 message passing (wood DP & amp; Combinatorics)
How to prevent overfitting in cross validation