java 8 Stream list to Map key 重复 value合并到Collection

阅读更多

关于把list转换成key value的map有很多博客上都有实现,这里是一个吧value放入到集合中去

 

      List list = Lists.newArrayList("1", "2", "3", "1");
        Map> map = list.stream().collect(Collectors.toMap(key -> key,
                value -> Lists.newArrayList(value),
                (List newValueList, List oldValueList) -> {
                    oldValueList.addAll(newValueList);
                    return oldValueList;
                }));
        System.out.println(JSON.toJSONString(map));

 类似的,你的list泛型可以是一个bean,取bean的属性当key或者value,转换成一个集合

 

结果: {"1":["1","1"],"2":["2"],"3":["3"]}

你可能感兴趣的:(Stream,list,to,map,java,8)