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

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


  1.   Map> partsMap = partList.stream().collect(Collectors.toMap(Parts::getMaintainCode, part ->
  2.   Lists.newArrayList(part),(List newValueList,List oldValueList)->
  3.        {oldValueList.addAll(newValueList);
  4.           return oldValueList;
  5.         }));

你可能感兴趣的:(Java 8 lambda Stream list to Map key 重复 value合并到Collection)