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

Java JDK8收集器新特性详解

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

     

干货文档

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

本课题设计了一种利用Matlab平台开发的植物叶片健康状态识别方案,重点融合了色彩与纹理双重特征以实现对叶片病害的自动化判别。该系统构建了直观的图形操作界面,便于用户提交叶片影像并快速获得分析结论。Matlab作为具备高效数值计算与数据处理能力的工具,在图像分析与模式分类领域应用广泛,本项目正是借助其功能解决农业病害监测的实际问题。 在色彩特征分析方面,叶片影像的颜色分布常与其生理状态密切相关。通常,健康的叶片呈现绿色,而出现黄化、褐变等异常色彩往往指示病害或虫害的发生。Matlab提供了一系列图像处理函数,例如可通过色彩空间转换与直方图统计来量化颜色属性。通过计算各颜色通道的统计参数(如均值、标准差及主成分等),能够提取具有判别力的色彩特征,从而为不同病害类别的区分提供依据。 纹理特征则用于描述叶片表面的微观结构与形态变化,如病斑、皱缩或裂纹等。Matlab中的灰度共生矩阵计算函数可用于提取对比度、均匀性、相关性等纹理指标。此外,局部二值模式与Gabor滤波等方法也能从多尺度刻画纹理细节,进一步增强病害识别的鲁棒性。 系统的人机交互界面基于Matlab的图形用户界面开发环境实现。用户可通过该界面上传待检图像,系统将自动执行图像预处理、特征抽取与分类判断。采用的分类模型包括支持向量机、决策树等机器学习方法,通过对已标注样本的训练,模型能够依据新图像的特征向量预测其所属的病害类别。 此类课题设计有助于深化对Matlab编程、图像处理技术与模式识别原理的理解。通过完整实现从特征提取到分类决策的流程,学生能够将理论知识与实际应用相结合,提升解决复杂工程问题的能力。总体而言,该叶片病害检测系统涵盖了图像分析、特征融合、分类算法及界面开发等多个技术环节,为学习与掌握基于Matlab的智能检测技术提供了综合性实践案例。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

dev666

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

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

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

打赏作者

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

抵扣说明:

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

余额充值