Stream流List转Map报错Duplicate key StreamMap

项目场景:

JDK8引入了Stream流,让程序员在开发中更方便进行集合之间的转换,在使用Stream流将List转为Map时,如果Map的key有重复的情况下,就会抛出java.lang.IllegalStateException: Duplicate key StreamMap这个异常。这个坑是不太容易被发现的,解决方法也非常简单,只需要在List转Map的时候指定,如果出现重复的Key,那么以哪个为最终的结果放入Map。


案列演示

这里我定义了一个SKU类,申明了三个SKU对象,其中有两个SKU对象的skuCode是一致的,我将按skuCode作为Mapkey

 @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Sku {

        private String skuName;

        private String skuCode;

        private BigDecimal weight;

    }
public class StreamMap {

    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    public static void main(String[] args) throws JsonProcessingException {

        Sku skuOne = Sku.builder().skuCode("bread").skuName("面包").weight(new BigDecimal(100.00)).build();
        Sku skuTwo = Sku.builder().skuCode("bread").skuName("面包").weight(new BigDecimal(110.00)).build();
        Sku skuThress = Sku.builder().skuCode("cheese").skuName("奶酪").weight(new BigDecimal(100.00)).build();
        List<Sku> skus = Arrays.asList(skuOne, skuTwo, skuThress);
        //以skuCode为key构建map
        Map<String, Sku> skuMap = skus.stream().collect(Collectors.toMap(Sku::getSkuCode, Function.identity()));
        System.out.println(OBJECT_MAPPER.writeValueAsString(skuMap));
    }
}

允许以上代码时,在使用Stream将List转Map时,由于List中skuCode相同的有多条,就会出现Duplicate key异常

Exception in thread "main" java.lang.IllegalStateException: Duplicate key StreamMap.Sku(skuName=面包, skuCode=bread, weight=100)

解决方案:

解决方案也非常简单,只需指定当List转Map时出现多个相同的key时,优先第一次出现那条数据还是最后出现的那条数据,比如本次案列中skuCodebread出现了两次,那么将由你来决定如果出现多条相同key的数据,是将第一条放入Map还是第二条放入Map。如果要将第一条放入Map,只需要优化一下代码如下:

public class StreamMap {

    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    public static void main(String[] args) throws JsonProcessingException {

        Sku skuOne = Sku.builder().skuCode("bread").skuName("面包").weight(new BigDecimal(100.00)).build();
        Sku skuTwo = Sku.builder().skuCode("bread").skuName("面包").weight(new BigDecimal(110.00)).build();
        Sku skuThress = Sku.builder().skuCode("cheese").skuName("奶酪").weight(new BigDecimal(100.00)).build();
        List<Sku> skus = Arrays.asList(skuOne, skuTwo, skuThress);
        //以skuCode为key构建map
//        Map skuMap = skus.stream().collect(Collectors.toMap(Sku::getSkuCode, Function.identity()));
        Map<String, Sku> skuMap = skus.stream().collect(Collectors.toMap(Sku::getSkuCode, Function.identity(), (o1, o2) -> o1));
        System.out.println(OBJECT_MAPPER.writeValueAsString(skuMap));
    }
}

打印出结果可以看到,weight为100的那条数据放入了Map
在这里插入图片描述
如果选择出现相同的key数据时,将最后出现的那条数据放入Map,代码如下:

public class StreamMap {

    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

    public static void main(String[] args) throws JsonProcessingException {

        Sku skuOne = Sku.builder().skuCode("bread").skuName("面包").weight(new BigDecimal(100.00)).build();
        Sku skuTwo = Sku.builder().skuCode("bread").skuName("面包").weight(new BigDecimal(110.00)).build();
        Sku skuThress = Sku.builder().skuCode("cheese").skuName("奶酪").weight(new BigDecimal(100.00)).build();
        List<Sku> skus = Arrays.asList(skuOne, skuTwo, skuThress);
        //以skuCode为key构建map
//        Map skuMap = skus.stream().collect(Collectors.toMap(Sku::getSkuCode, Function.identity()));
        Map<String, Sku> skuMap = skus.stream().collect(Collectors.toMap(Sku::getSkuCode, Function.identity(), (o1, o2) -> o2));
        System.out.println(OBJECT_MAPPER.writeValueAsString(skuMap));
    }
}

打印出结果可以看到,weight为110的那条数据放入了Map
Stream流List转Map报错Duplicate key StreamMap_第1张图片
如果要使用Stream将List转为Map,那么应该注意List中有相同Key出现的情况,避免出现Duplicate key 这种情况

你可能感兴趣的:(list,java,stream)