Map实现集合中相同属性元素(合并去重)

               HashMap<String, LogisticsShipmentDTO.LogisticsShipmentOpt> map = Maps.newHashMap();
                            //根据sku+ 市场--合并入库数量及总运费
                            shipmentOptList.stream().forEach(shipmentOpt -> {
                                LogisticsShipmentDTO.LogisticsShipmentOpt opt = map.get(shipmentOpt.getSku());
                                if (Objects.nonNull(opt)) {
                                    shipmentOpt.setQty(new BigDecimal(shipmentOpt.getQty()).add(new BigDecimal(opt.getQty())).toString());
                                    shipmentOpt.setTotalFeight(shipmentOpt.getTotalFeight().subtract(opt.getTotalFeight()));
                                    map.put(shipmentOpt.getSku(), shipmentOpt);
                                } else {
                                    map.put(shipmentOpt.getSku(), shipmentOpt);
                                }
                            });

 

posted @ 2021-06-22 14:09  这很周锐  阅读(459)  评论(0编辑  收藏  举报