目录
一、groupingBy
1、groupingBy介绍
2、groupingBy使用
一、groupingBy 1、groupingBy介绍
roupingBy()是Stream API中最强大的收集器Collector之一,提供与SQL的GROUP BY子句类似的功能。
2、groupingBy使用
1、对整个字段进行分组查询:直接groupingBy那个字段就可以了
2、对字段的一部分进行分组查询:s->s.getId().substring(0, 3),获取到这个字段后进行字符串截取。
3、对每个分组的数据个数进行统计:Collectors.groupingBy(UserEntity::getId,Collectors.counting())
4、对id字段进行分组并获取age的平均数:
Collectors.groupingBy(UserEntity::getId,Collectors.averagingInt(UserEntity::getAge))
5、对id字段进行分组并获取age的总值:
Collectors.groupingBy(UserEntity::getId, Collectors.summingInt(UserEntity::getAge))
public class Stream { public static void main(String[] args) { groupById(); // 对id字段进行分组查询 groupByPartId(); // 对id字段的部分进行分组查询 groupByIdGetCount(); // 对每个分组的数据个数进行统计 groupByIdGetAgeAve(); // 对id字段进行分组并获取age的平均数 groupByIdGetAgeSum(); // 对id字段进行分组并获取age的总值 } public static void groupById(){ UserEntity user1 = new UserEntity("1001", "zhangsan", 18, "123"); UserEntity user2 = new UserEntity("1001", "lisi", 20, "123"); UserEntity user3 = new UserEntity("1001", "wangwu", 15, "123"); UserEntity user4 = new UserEntity("2001", "张三", 22, "123"); UserEntity user5 = new UserEntity("2001", "李四", 21, "123"); UserEntity user6 = new UserEntity("2001", "王五",23, "123"); List list = new ArrayList<>(); list.add(user1); list.add(user2); list.add(user3); list.add(user4); list.add(user5); list.add(user6); Map> map = list.stream().collect(Collectors.groupingBy(UserEntity::getId)); map.forEach((key, value)->{ System.out.println(key+":"+value); }); } public static void groupByPartId(){ UserEntity user1 = new UserEntity("1001", "zhangsan", 19, "123"); UserEntity user2 = new UserEntity("1002", "lisi", 17, "123"); UserEntity user3 = new UserEntity("1003", "wangwu", 18, "123"); UserEntity user4 = new UserEntity("2001", "张三", 28, "123"); UserEntity user5 = new UserEntity("2002", "李四", 23, "123"); UserEntity user6 = new UserEntity("2003", "王五", 26, "123"); List list = new ArrayList<>(); list.add(user1); list.add(user2); list.add(user3); list.add(user4); list.add(user5); list.add(user6); Map> map = list.stream().collect(Collectors.groupingBy(s->s.getId().substring(0, 3))); map.forEach((key, value)->{ System.out.println(key+":"+value); }); } private static void groupByIdGetCount() { UserEntity user1 = new UserEntity("1001", "zhangsan", 18, "123"); UserEntity user2 = new UserEntity("1001", "lisi", 20, "123"); UserEntity user3 = new UserEntity("1001", "wangwu", 15, "123"); UserEntity user4 = new UserEntity("2001", "张三", 22, "123"); UserEntity user5 = new UserEntity("2001", "李四", 21, "123"); UserEntity user6 = new UserEntity("2001", "王五",23, "123"); List list = new ArrayList<>(); list.add(user1); list.add(user2); list.add(user3); list.add(user4); list.add(user5); list.add(user6); Map map = list.stream().collect(Collectors.groupingBy(UserEntity::getId, Collectors.counting())); map.forEach((key, value)->{ System.out.println(key+":"+value); }); } private static void groupByIdGetAgeAve() { UserEntity user1 = new UserEntity("1001", "zhangsan", 18, "123"); UserEntity user2 = new UserEntity("1001", "lisi", 20, "123"); UserEntity user3 = new UserEntity("1001", "wangwu", 15, "123"); UserEntity user4 = new UserEntity("2001", "张三", 22, "123"); UserEntity user5 = new UserEntity("2001", "李四", 21, "123"); UserEntity user6 = new UserEntity("2001", "王五",23, "123"); List list = new ArrayList<>(); list.add(user1); list.add(user2); list.add(user3); list.add(user4); list.add(user5); list.add(user6); Map map = list.stream().collect(Collectors.groupingBy(UserEntity::getId, Collectors.averagingInt(UserEntity::getAge))); map.forEach((key, value)->{ BigDecimal bigDecimal = new BigDecimal(value); bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP); System.out.println(key+":"+value); // 要想保留两位小数 使用bigDecimal, 四舍五入 }); } private static void groupByIdGetAgeSum() { UserEntity user1 = new UserEntity("1001", "zhangsan", 18, "123"); UserEntity user2 = new UserEntity("1001", "lisi", 20, "123"); UserEntity user3 = new UserEntity("1001", "wangwu", 15, "123"); UserEntity user4 = new UserEntity("2001", "张三", 22, "123"); UserEntity user5 = new UserEntity("2001", "李四", 21, "123"); UserEntity user6 = new UserEntity("2001", "王五",23, "123"); List list = new ArrayList<>(); list.add(user1); list.add(user2); list.add(user3); list.add(user4); list.add(user5); list.add(user6); Map map = list.stream().collect(Collectors.groupingBy(UserEntity::getId, Collectors.summingInt(UserEntity::getAge))); map.forEach((key, value)->{ System.out.println(key+":"+value); // 要想保留两位小数 使用bigDecimal, 四舍五入 }); }}