Streams Java8: Group By List values

 User user =new User();

user.setId(1);
user.setName("test");
List<User> users =new ArrayList<>();
users.add(user);
user =new User();
user.setId(1);
user.setName("test1");
users.add(user);

Map<Integer,List<String>> params=
users.stream().collect(Collectors.groupingBy(User::getId,
Collectors.mapping(User::getName,Collectors.toList())));

//List<User>
Map<Integer,List<User>> params1=
users.stream().collect(Collectors.groupingBy(User::getId));

Comments