小滴课堂-学习笔记:(7) Java高级核心之玩转 JDK8 收集器和集合统计

56 篇文章 0 订阅
20 篇文章 0 订阅

logo 愿景:"让编程不再难学,让技术与生活更加有趣"


更多架构课程请访问 xdclass.net

 

目录

第1集 Java新特性玩转JDK8之collector收集器

第2集 Java新特性玩转JDK8之joining函数

第4集 Java新特性玩转JDK8之收集器 group by分组

第5集 Java新特性玩转JDK8之收集器 group by进阶

第6集 Java新特性玩转JDK8之summarizing集合统计

干货文档


第1集 Java新特性玩转JDK8之collector收集器

简介:讲解jdk8里面的收集器collector

  • collect()方法的作用

    • 一个终端操作, 用于对流中的数据进行归集操作,collect方法接受的参数是一个Collector

    • 有两个重载方法,在Stream接口里面

       //重载方法一
       <R> R collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator, BiConsumer<R, R>combiner);
       
        //重载方法二
       <R, A> R collect(Collector<? super T, A, R> collector);

       

    • Collector的作用

      • 就是收集器,也是一个接口, 它的工具类Collectors提供了很多工厂方法

       

    • Collectors 的作用

      • 工具类,提供了很多常见的收集器实现

        • Collectors.toList()

          public static <T> Collector<T, ?, List<T>> toList() {
          ​
          return new CollectorImpl<>((Supplier<List<T>>) ArrayList::new,        List::add,(left, right) -> { left.addAll(right); return left; }, CH_ID);
          ​
          }
          • ArrayList::new,创建一个ArrayList作为累加器

          • List::add,对流中元素的操作就是直接添加到累加器中

          • reduce操作, 对子任务归集结果addAll,后一个子任务的结果直接全部添加到前一个子任务结果中

          • CH_ID 是一个unmodifiableSet集合

        • Collectors.toMap()

        • Collectors.toSet()

        • Collectors.toCollection() :用自定义的实现Collection的数据结构收集

          • Collectors.toCollection(LinkedList::new)

          • Collectors.toCollection(CopyOnWriteArrayList::new)

          • Collectors.toCollection(TreeSet::new)

           

 

 

第2集 Java新特性玩转JDK8之joining函数

简介:讲解jdk8里面的收集器joining函数

  • 拼接函数 Collectors.joining

//3种重载方法
Collectors.joining()
Collectors.joining("param")
Collectors.joining("param1", "param2", "param3")
  • 其中一个的实现

    public static Collector<CharSequence, ?, String> joining() {
        return new CollectorImpl<CharSequence, StringBuilder, String>(
                StringBuilder::new, StringBuilder::append,
                (r1, r2) -> { r1.append(r2); return r1; },
                StringBuilder::toString, CH_NOID);
    }
  • 说明:

    • 该方法可以将Stream得到一个字符串, joining函数接受三个参数,分别表示 元素之间的连接符、前缀和后缀。

    String result = Stream.of("springboot", "mysql", "html5", "css3").collect(Collectors.joining(",", "[", "]"));
    
    
    

     

 

第3集 Java新特性玩转JDK8之收集器 partitioningBy分组

简介:讲解jdk8里面的收集器partitioningBy分组

  • Collectors.partitioningBy 分组,key是boolean类型

    public static <T>
    Collector<T, ?, Map<Boolean, List<T>>> partitioningBy(Predicate<? super T> predicate) {
        return partitioningBy(predicate, toList());
    }

     

  • 练习: 根据list里面进行分组,字符串长度大于4的为一组,其他为另外一组

    List<String> list = Arrays.asList("java", "springboot", "HTML5","nodejs","CSS3");
    Map<Boolean, List<String>> result = list.stream().collect(partitioningBy(obj -> obj.length() > 4));
    
    
    
    
    

     

第4集 Java新特性玩转JDK8之收集器 group by分组

简介:讲解jdk8里面的收集器 group by分组

  • 分组 Collectors.groupingBy()

public static <T, K> Collector<T, ?, Map<K, List<T>>> groupingBy(Function<? super T, ? extends K> classifier) { return groupingBy(classifier, toList()); }
  • 练习:根据学生所在的省份,进行分组

    List<Student> students = Arrays.asList(new Student("广东", 23), new Student("广东", 24), new Student("广东", 23),new Student("北京", 22), new Student("北京", 20), new Student("北京", 20),new Student("海南", 25));
    ​
    ​
    Map<String, List<Student>> listMap = students.stream().collect(Collectors.groupingBy(obj -> obj.getProvince()));
    ​
            listMap.forEach((key, value) -> {
                System.out.println("========");
                System.out.println(key);
                value.forEach(obj -> {
                    System.out.println(obj.getAge());
                });
    ​
            });
    ​
    ​
    ​
    class Student {
        private String province;
        private int age;
    ​
        public String getProvince() {
            return province;
        }
    ​
        public void setProvince(String province) {
            this.province = province;
        }
    ​
        public int getAge() {
            return age;
        }
    ​
        public void setAge(int age) {
            this.age = age;
        }
    ​
    ​
        public Student(String province, int age) {
            this.age = age;
            this.province = province;
        }
    ​
    }
    
    
    

     

 

第5集 Java新特性玩转JDK8之收集器 group by进阶

简介:讲解jdk8里面的收集器 group by进阶

  • 分组统计

    • 聚合函数进行统计查询,分组后统计个数

    • Collectors.counting() 统计元素个数

    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);
        }
    

     

  • 需求:统计各个省份的人数

     List<Student> students = Arrays.asList(new Student("广东", 23), new Student("广东", 24), new Student("广东", 23),new Student("北京", 22), new Student("北京", 20), new Student("北京", 20),new Student("海南", 25));
    ​
    ​
    Map<String, Long> listMap = students.stream().collect(Collectors.groupingBy(Student::getProvince, Collectors.counting()));
    ​
    listMap.forEach((key, value) -> {System.out.println(key+"省人数有 "+value);});
    
    
    

     

第6集 Java新特性玩转JDK8之summarizing集合统计

简介:讲解jdk8里面的收集器 group by进阶

  • summarizing 统计相关, 下面是summarizingInt的源码

    public static <T> Collector<T, ?, IntSummaryStatistics> summarizingInt(ToIntFunction<? super T> mapper) { return new CollectorImpl<T, IntSummaryStatistics, IntSummaryStatistics>(
                    IntSummaryStatistics::new,
                    (r, t) -> r.accept(mapper.applyAsInt(t)),
                    (l, r) -> { l.combine(r); return l; }, CH_ID);
        }
    • 作用:可以一个方法把统计相关的基本上都完成

    • 分类

      • summarizingInt

      • summarizingLong

      • summarizingDouble

  • 需求:统计学生的各个年龄信息

     List<Student> students = Arrays.asList(new Student("广东", 23), new Student("广东", 24), new Student("广东", 23),new Student("北京", 22), new Student("北京", 20), new Student("北京", 20),new Student("海南", 25));
    ​
    IntSummaryStatistics summaryStatistics = students.stream().collect(Collectors.summarizingInt(Student::getAge));
    System.out.println("平均值:" + summaryStatistics.getAverage());
    System.out.println("人数:" + summaryStatistics.getCount()); 
    System.out.println("最大值:" + summaryStatistics.getMax());
    System.out.println("最小值:" + summaryStatistics.getMin());
    System.out.println("总和:" + summaryStatistics.getSum());
    ​
    ​
    class Student {
        private String province;
        private int age;
    ​
        public String getProvince() {
            return province;
        }
    ​
        public void setProvince(String province) {
            this.province = province;
        }
    ​
        public int getAge() {
            return age;
        }
    ​
        public void setAge(int age) {
            this.age = age;
        }
    ​
    ​
        public Student(String province, int age) {
            this.age = age;
            this.province = province;
        }
    ​
    }
    
    
    

     

干货文档

                                                        关注公众号发送:“CSDN干货文档”  即可领取

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dev666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值