当前位置:网站首页>开发中对集合里面的数据根据属性进行合并数量时犯的错误
开发中对集合里面的数据根据属性进行合并数量时犯的错误
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());这样就不对原对象进行操作了,所以问题就解决了
边栏推荐
- WCF基金会
- 初识MySQL
- 今日问题-2022/7/4 lambda体中修改String引用类型变量
- 各种语言,软件,系统的国内镜像,收藏这一个仓库就够了: Thanks-Mirror
- JS es5 peut également créer des constantes?
- Blue Bridge Cup 2022 13th provincial competition real topic - block painting
- Machine learning: the difference between random gradient descent (SGD) and gradient descent (GD) and code implementation.
- ZOJ Problem Set – 2563 Long Dominoes 【如压力dp】
- Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period
- swiper组件中使用video导致全屏错位
猜你喜欢

Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period

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

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

今日问题-2022/7/4 lambda体中修改String引用类型变量

黑马笔记---创建不可变集合与Stream流

The cradle of eternity

Scenario practice: quickly build wordpress blog system based on function calculation

Appium foundation - appium inspector positioning tool (I)

AcWing 345. Cattle station solution (nature and multiplication of Floyd)

从底层结构开始学习FPGA----FIFO IP的定制与测试
随机推荐
AI automatically generates annotation documents from code
json学习初体验–第三者jar包实现bean、List、map创json格式
Yunna | work order management software, work order management software app
tansig和logsig的差异,为什么BP喜欢用tansig
AcWing 361. Sightseeing cow problem solution (SPFA seeking positive ring)
永久的摇篮
Yiwen takes you into [memory leak]
Add PDF Title floating window
AcWing 345. 牛站 题解(floyd的性质、倍增)
长按按钮执行函数
增加 pdf 标题浮窗
设置Wordpress伪静态连接(无宝塔)
Transplant DAC chip mcp4725 to nuc980
糊涂工具类(hutool)post请求设置body参数为json数据
How to prevent overfitting in cross validation
C language instance_ three
AcWing 904. 虫洞 题解(spfa求负环)
Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period
The cradle of eternity
JVM memory model