来看看Java stream提供的分组 - groupingBy
一. 基本用法 - 接收一个参数
它接收一个函数作为参数,也就是说可以传lambda表达式进来。
public static <T, K> Collector<T, ?, Map<K, List<T>>>
groupingBy(Function<? super T, ? extends K> classifier) {
return groupingBy(classifier, toList());
}
UserList.stream().collect(Collectors.groupingBy( 函数 ))
,返回的是一个map,key为分组的值,value为list,包含组内的元素。
1. 简单类型分组
@Test
public void test01() {
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 1, 2, 3);
Map<Integer, List<Integer>> collect = intList.stream().collect(Collectors.groupingBy(e -> e%2));
System.out.println(collect);
}
2. 对象,按一个属性分组
这里用到的User::getEducation
,class名字+双冒号+方法名,等同于lambda表达式e -> e.getEduction()
void test02() {
List<User> userList = Arrays.asList(new User(0, "zhangsan", "zhangsan@qq.com", 20, "High School"),
new User(1, "lisi", "lisi@qq.com", 20, "High School"),
new User(2, "wangwu", "wangwu@qq.com", 20, "High School"),
new User(3, "lilei", "lilei@qq.com", 25, "Graduate"),
new User(4, "hanmeimei", "hanmeimei@qq.com", 26, "Graduate"),
new User(5, "lucy", "lucy@qq.com", 25, "Graduate"));
Map<String, List<User>> collect = userList.stream().collect(Collectors.groupingBy(User::getEducation));
System.out.println(collect);
}
public
3. 对象,按多个属性分组
嵌套分组 (groupingBy 接收两个参数)
Map<Integer, Map<String, List<User>>> collect =
userList.stream().collect(Collectors.groupingBy(User::getAge, Collectors.groupingBy(User::getEducation)));
System.out.println(collect.get(20).get("High School"));
4. 按整个对象分组
用User::new
方法进行分组,它会调用构造方法public User(User user)
,并依赖于hashcode
和equals
方法来判断对象是否相等,进而进行分组。如果有一些特殊的逻辑对对象进行分组,可以重写hashcode
和equals
方法。
Map<User, List<User>> collect = userList.stream().collect(Collectors.groupingBy(User::new));
public User(User user) {
this.id = user.id;
this.name = user.name;
this.email = user.email;
this.age = user.age;
this.education = user.education;
}
5. 处理null key
Map<String, List<User>> collect = userList.stream().collect(Collectors.groupingBy(e -> {
if (e.getEducation() == null || "".equals(e.getEducation())) {
return "";
}
return e.getEducation();
}));
二. 接收两个参数 - 对groupingby结果集进一步处理
public static <T, K, A, D>
Collector<T, ?, Map<K, D>> groupingBy(Function<? super T, ? extends K> classifier,
Collector<? super T, A, D> downstream) {
return groupingBy(classifier, HashMap::new, downstream);
}
1. 计算平均值
Map<String, Double> collect =
userList.stream().collect(Collectors.groupingBy(User::getEducation, Collectors.averagingInt(User::getAge)));
{High School=20.0, Graduate=25.333333333333332}
2. count
Map<String, Long> collect =
userList.stream().collect(Collectors.groupingBy(User::getEducation, Collectors.counting()));
{High School=3, Graduate=3}
3. 求和
Map<String, Integer> collect =
userList.stream().collect(Collectors.groupingBy(User::getEducation, Collectors.summingInt(User::getAge)));
{High School=60, Graduate=76}
三. 接收三个参数
添加了对结果Map的生成方式,默认是HashMap
public static <T, K, D, A, M extends Map<K, D>>
Collector<T, ?, M> groupingBy(Function<? super T, ? extends K> classifier,
Supplier<M> mapFactory,
Collector<? super T, A, D> downstream)
例,用TreeMap对key进行排序:
Map<String, Set<String>> namesByEdu =
userList.stream().collect(Collectors.groupingBy(User::getEducation, TreeMap::new, mapping(User::getName,Collectors.toSet())));
System.out.println(namesByEdu);
{Graduate=[lilei, hanmeimei, lucy], High School=[lisi, zhangsan]}