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

  1. List<String> list = Lists.newArrayList(“1”“2”“3”“1”);  
  2.   Map<String, List<String>> map = list.stream().collect(Collectors.toMap(key -> key,  
  3.           value -> Lists.newArrayList(value),  
  4.           (List<String> newValueList, List<String> oldValueList) -> {  
  5.               oldValueList.addAll(newValueList);  
  6.               return oldValueList;  
  7.           }));  
  8.   System.out.println(JSON.toJSONString(map));  
  1.   Map<String,List<Parts>> partsMap = partList.stream().collect(Collectors.toMap(Parts::getMaintainCode, part ->
  2.   Lists.newArrayList(part),(List<Parts> newValueList,List<Parts> oldValueList)->
  3.        {oldValueList.addAll(newValueList);
  4.           return oldValueList;
  5.         }));
    原文作者:feiwuguohai
    原文地址: https://blog.csdn.net/feiwuguohai/article/details/80522998
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞