1 User实体类
public class User {
private Long userId;
private String username;
private int age;
private int sex;
private String password;
}
2 单字段分组
List<User> userList = new ArrayList<>();
Map<Integer, List<User>> ageGroupMap = userList.stream().collect(Collectors.groupingBy(User::getAge));
Map<Integer, List<User>> ageGroupMap2 = userList.stream().collect(Collectors.groupingBy(x->x.getAge()));
3 多字段分组
List<User> userList = new ArrayList<>();
Map<String, List<User>> ageGroupMap = userList.stream().collect(Collectors.groupingBy(x -> x.getAge() + "#" + x.getSex()));
分完组之后key是这几个拼成的string。
|