当前位置:网站首页>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

原网站

版权声明
本文为[Ant HJK]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207061814144795.html