悠然哈哈哈

导航

jdk 8 list 转map Duplicate key 报错

 

根据状态分组

//原来写法
Map<String, SoDetail> upDetailMap = upSoDetailList.stream().collect(Collectors.toMap(SoDetail::getStatus, item -> item));

//正确写法

Map<String, SoDetail> upDetailMap = upSoDetailList.stream().collect(Collectors.toMap(SoDetail::getStatus, item -> item,(k1, k2) -> k1));

 

 orderLines = new ArrayList<>(orderLines.stream()
            .collect(Collectors.toMap(OrderLine::getProductId, a -> a, (o1, o2) -> {
                double qty = o1.getQuantity() ==null ? 0.0: o1.getQuantity();
                double qty2 = o2.getQuantity() ==null ? 0.0: o2.getQuantity();
                o1.setQuantity(qty + qty2);
                return o1;
            })).values());
// 多个字段合并key 统计qty

itemDtos = new ArrayList<>(itemDtos .stream()
              
                .collect(Collectors.toMap(s -> (StringUtils.isBlank(s.getLotAtt05()) ? String.join("_", s.getOwnerCode(), s.getSkuCode(), s.getLotAtt04()):String.join("_", s.getOwnerCode(), s.getSkuCode(), s.getLotAtt04(), s.getLotAtt05())) , a -> a, (o1, o2) -> {
                    BigDecimal qty = o1.getQty() ==null ? BigDecimal.ZERO: o1.getQty();
                    BigDecimal qty2 = o2.getQty() ==null ? BigDecimal.ZERO: o2.getQty();
                    o1.setQty(qty.add(qty2));
                    return o1;
                })).values());

 

posted on 2021-08-25 09:07  悠然886  阅读(102)  评论(0编辑  收藏  举报