Java Stream分组groupBy后,取某个字段值为字符串或者list
某个字段值为字符串
List<User> userList = new ArrayList(); Map<String,String> collect = userList.stream().collect(Collectors.groupingBy( User::getRealname, Collectors.mapping(User::getUsername, Collectors.joining(","))));
某个字段值为list
List<User> userList = new ArrayList(); Map<String,List<String>> collect = userList.stream().collect(Collectors.groupingBy( User::getRealname, Collectors.mapping(User::getUsername, Collectors.toList())));