Java流式操作之Collectors工具類操作指南
方法
● maxBy:獲取流中最大元素;minBy:獲取流中最小元素
● joining:合并,將流中的元素,以字符串的形式拼接起來
● summingInt:把流中的元素映射成int類型的元素,求和
● averagingInt:把流中的元素映射成int類型的元素,求平均值
● summarizingInt:把流中的元素映射成int類型的元素,獲取描述信息
實(shí)踐說明
一、前提條件
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實(shí)體類
* @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;
//向下轉(zhuǎn)型,再去比較屬性值
Person person = (Person) o;
//如果屬性值相同,最后的結(jié)果為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對象的姓名拼接成一個(gè)字符串
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類型的元素,獲取描述信息
需求:將流中分?jǐn)?shù)大于等于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());輸出結(jié)果:

總結(jié)
到此這篇關(guān)于Java流式操作之Collectors工具類操作的文章就介紹到這了,更多相關(guān)Java流式操作Collectors工具類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot整合Spring Data Elasticsearch的過程詳解
這篇文章主要介紹了SpringBoot整合Spring Data Elasticsearch的過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
IntelliJ IDEA之配置JDK的4種方式(小結(jié))
這篇文章主要介紹了IntelliJ IDEA之配置JDK的4種方式(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
spring-boot-maven-plugin報(bào)紅解決方案(親測有效)
本文主要介紹了spring-boot-maven-plugin報(bào)紅解決方案,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Java多線程編程之ThreadLocal線程范圍內(nèi)的共享變量
這篇文章主要介紹了Java多線程編程之ThreadLocal線程范圍內(nèi)的共享變量,本文講解了ThreadLocal的作用和目的、ThreadLocal的應(yīng)用場景、ThreadLocal的使用實(shí)例等,需要的朋友可以參考下2015-05-05
Java中使用MyBatis-Plus操作數(shù)據(jù)庫的實(shí)例
本文主要介紹了Java中使用MyBatis-Plus操作數(shù)據(jù)庫的實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02
POI對Excel自定義日期格式的讀取(實(shí)例代碼)
下面小編就為大家?guī)硪黄狿OI對Excel自定義日期格式的讀取(實(shí)例代碼)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11
Java單例模式利用HashMap實(shí)現(xiàn)緩存數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了Java單例模式利用HashMap實(shí)現(xiàn)緩存數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04

