Java Collectors工具类怎么使用
方法
● maxBy:获取流中最大元素;minBy:获取流中最小元素
● joining:合并,将流中的元素,以字符串的形式拼接起来
● summingInt:把流中的元素映射成int类型的元素,求和
● averagingInt:把流中的元素映射成int类型的元素,求平均值
● summarizingInt:把流中的元素映射成int类型的元素,获取描述信息
实践说明
一、前提条件
Person类
package com.example; import lombok.AllArgsConstructor; import lombok.Data; import org.springframework.context.annotation.Configuration; import java.util.Objects; /** * @BelongsProject: StreamOperate * @BelongsPackage: com.example * @CreateTime: 2023-05-01 11:18 * @Description: Person实体类 * @Version: 1.0 */ public class Person implements Comparable<Person>{ public String getName() { return name; } public Person setName(String name) { this.name = name; return this; } public int getAge() { return age; } public Person setAge(int age) { this.age = age; return this; } public int getScore() { return score; } public Person setScore(int score) { this.score = score; return this; } private String name; private int age; private int score; public Person(String name, int age, int score) { this.name = name; this.age = age; this.score = score; } public Person() { } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", score=" + score + '}'; } @Override public boolean equals(Object o) { //地址相同,为true if (this == o) return true; //为null,并且类型不一样,为false if (o == null || getClass() != o.getClass()) return false; //向下转型,再去比较属性值 Person person = (Person) o; //如果属性值相同,最后的结果为true return age == person.age && score == person.score && Objects.equals(name, person.name); //return false; } @Override public int hashCode() { return Objects.hash(name, age, score); } @Override public int compareTo(Person o) { return this.getScore()-o.getScore(); } }
Data类
package com.example; import org.springframework.context.annotation.Configuration; import java.util.ArrayList; /** * @BelongsProject: StreamOperate * @BelongsPackage: com.example * @CreateTime: 2023-05-01 11:08 * @Description: Data类 * @Version: 1.0 */ public class Data { public static ArrayList<Person> getData() { ArrayList<Person> personList = new ArrayList<>(); personList.add(new Person("张三", 18, 90)); personList.add(new Person("李四", 19, 100)); personList.add(new Person("王五", 17, 60)); personList.add(new Person("赵六", 18, 89)); personList.add(new Person("孙七", 20, 96)); personList.add(new Person("郑十", 20, 46)); personList.add(new Person("周八", 20, 96)); personList.add(new Person("周八", 20, 96)); personList.add(new Person("吴九", 20, 45)); personList.add(new Person("邓十一", 20, 35)); personList.add(new Person("刘十二", 20, 99)); personList.add(new Person("小十三", 20, 56)); personList.add(new Person("小十三", 20, 56)); return personList; } }
二、操作
maxBy:获取流中最大元素;minBy:获取流中最小元素
public static void main(String[] args) { Stream<Person> stream = Data.getData().stream(); //maxBy:获取流中最大元素;minBy:获取流中最小元素 System.out.println(Data.getData().stream().collect(Collectors.maxBy((ele1, ele2) -> ele1.getScore() - ele2.getScore()))); System.out.println(Data.getData().stream().collect(Collectors.minBy((ele1, ele2) -> ele1.getAge() - ele2.getAge()))); }
joining:合并,将流中的元素,以字符串的形式拼接起来
public static void main(String[] args) { Stream<Person> stream = Data.getData().stream(); //joining:合并,将流中的元素,以字符串的形式拼接起来 //将集合中person对象的姓名拼接成一个字符串 System.out.println(Data.getData().stream().map(Person::getName).collect(Collectors.joining())); System.out.println(Data.getData().stream().map(Person::getName).collect(Collectors.joining("-"))); System.out.println(Data.getData().stream().map(Person::getName).collect(Collectors.joining("-", "{", "}"))); }
summingInt:把流中的元素映射成int类型的元素,求和
public static void main(String[] args) { Stream<Person> stream = Data.getData().stream(); //summingInt:把流中的元素映射成int类型的元素,求和 System.out.println(Data.getData().stream().collect(Collectors.summingInt(Person::getScore))); }
averagingInt:把流中的元素映射成int类型的元素,求平均值
public static void main(String[] args) { Stream<Person> stream = Data.getData().stream(); //averagingInt:把流中的元素映射成int类型的元素,求平均值 System.out.println(Data.getData().stream().collect(Collectors.averagingInt(Person::getScore))); }
summarizingInt:把流中的元素映射成int类型的元素,获取描述信息
需求:将流中分数大于等于80的Person对象替换成他们的姓名
public static void main(String[] args) { Stream<Person> stream = Data.getData().stream(); //summarizingInt:把流中的元素映射成int类型的元素,获取描述信息 IntSummaryStatistics collect = Data.getData().stream().collect(Collectors.summarizingInt(Person::getScore)); System.out.println(collect); System.out.println(collect.getCount()); System.out.println(collect.getSum()); System.out.println(collect.getMax()); System.out.println(collect.getMax()); System.out.println(collect.getAverage());
输出结果:
以上是Java Collectors工具类怎么使用的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

胶囊是一种三维几何图形,由一个圆柱体和两端各一个半球体组成。胶囊的体积可以通过将圆柱体的体积和两端半球体的体积相加来计算。本教程将讨论如何使用不同的方法在Java中计算给定胶囊的体积。 胶囊体积公式 胶囊体积的公式如下: 胶囊体积 = 圆柱体体积 两个半球体体积 其中, r: 半球体的半径。 h: 圆柱体的高度(不包括半球体)。 例子 1 输入 半径 = 5 单位 高度 = 10 单位 输出 体积 = 1570.8 立方单位 解释 使用公式计算体积: 体积 = π × r2 × h (4

Java是热门编程语言,适合初学者和经验丰富的开发者学习。本教程从基础概念出发,逐步深入讲解高级主题。安装Java开发工具包后,可通过创建简单的“Hello,World!”程序实践编程。理解代码后,使用命令提示符编译并运行程序,控制台上将输出“Hello,World!”。学习Java开启了编程之旅,随着掌握程度加深,可创建更复杂的应用程序。
