Java流式操作之Collectors工具类操作指南
作者:吃豆子的恐龙 时间:2022-12-01 01:25:55
方法
● 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());
输出结果:
来源:https://blog.csdn.net/weixin_43319713/article/details/130462745
标签:流式,collectors,工具类
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
用Kotlin实现Android点击事件的方法
2023-01-16 15:56:03
Android利用CircleImageView实现圆形头像的方法
2023-06-08 12:06:52
![](https://img.aspxhome.com/file/2023/9/92579_0s.jpg)
Android中的全局变量与局部变量使用小结
2023-12-07 10:11:49
![](https://img.aspxhome.com/file/2023/8/130318_0s.jpg)
C#获取Windows进程监听的TCP/UDP端口实例
2021-11-20 13:06:21
JVM内存管理之JAVA语言的内存管理详解
2021-11-01 12:00:10
![](https://img.aspxhome.com/file/2023/5/63105_0s.jpg)
Android仿微信清理内存图表动画(解决surfaceView屏幕闪烁问题)demo实例详解
2023-05-31 15:35:27
![](https://img.aspxhome.com/file/2023/7/111737_0s.gif)
浅谈Android串口通讯SerialPort原理
2023-03-31 12:39:52
![](https://img.aspxhome.com/file/2023/0/117080_0s.png)
System.currentTimeMillis()计算方式与时间的单位转换详解
2022-08-09 04:55:43
C#几种截取字符串的方法小结
2023-07-16 09:55:10
Java设计模式之工厂模式案例详解
2023-11-27 20:08:03
![](https://img.aspxhome.com/file/2023/2/60342_0s.png)
Java NIO实现多人聊天室
2022-04-05 09:03:17
![](https://img.aspxhome.com/file/2023/4/62964_0s.jpg)
MyBatis 中 SqlMapConfig 配置文件详解
2023-10-19 21:23:01
Java多线程--让主线程等待所有子线程执行完毕在执行
2022-09-14 01:21:34
Flutter应用框架搭建实现屏幕适配方案详解
2023-09-19 08:16:27
java关于字符串的常用API
2022-09-13 22:19:03
![](https://img.aspxhome.com/file/2023/4/79844_0s.png)
专属于程序员的浪漫-Java输出动态闪图iloveyou
2023-12-01 16:00:57
![](https://img.aspxhome.com/file/2023/8/84608_0s.png)
Java的静态类型检查示例代码详解
2023-08-09 10:42:13
C#中登录窗体和欢迎窗体关闭方法分析
2023-03-23 03:58:25
java异步上传图片示例
2023-05-31 13:42:51
SpringBoot整合Shiro框架,实现用户权限管理
2021-10-27 00:03:02