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)文章
java構(gòu)造函數(shù)示例(構(gòu)造方法)
這篇文章主要介紹了java構(gòu)造函數(shù)示例(構(gòu)造方法),需要的朋友可以參考下2014-03-03
使用Spring Data R2DBC +Postgres實(shí)現(xiàn)增刪改查功能
這篇文章主要介紹了使用Spring Data R2DBC +Postgres實(shí)現(xiàn)增刪改查功能,本文通過兩種方法給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
Java生成遞增流水號(編號+時(shí)間+流水號)簡單示例
這篇文章主要給大家介紹了關(guān)于Java生成遞增流水號(編號+時(shí)間+流水號)的相關(guān)資料,在開發(fā)項(xiàng)目漫長的過程中常常會(huì)遇到流水號需要自動(dòng)生成的問題存在,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下2023-07-07
Java網(wǎng)絡(luò)編程教程之設(shè)置請求超時(shí)的方法
這篇文章主要給大家介紹了關(guān)于Java網(wǎng)絡(luò)編程教程之設(shè)置請求超時(shí)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
IDEA神器一鍵查看Java字節(jié)碼及其他類信息插件
這篇文章主要為大家介紹了一款I(lǐng)DEA神器,可以一鍵查看Java字節(jié)碼及其他類信息,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-01-01
JavaWeb實(shí)現(xiàn)學(xué)生管理系統(tǒng)的超詳細(xì)過程
學(xué)生信息管理系統(tǒng)是針對學(xué)校人事處的大量業(yè)務(wù)處理工作而開發(fā)的管理軟件,主要用于學(xué)校學(xué)生信息管理,下面這篇文章主要給大家介紹了關(guān)于JavaWeb實(shí)現(xiàn)學(xué)生管理系統(tǒng)的超詳細(xì)過程,需要的朋友可以參考下2023-05-05
SpringBoot搭建Dubbo項(xiàng)目實(shí)現(xiàn)斐波那契第n項(xiàng)詳解
這篇文章主要講解了“SpringBoot+Dubbo怎么實(shí)現(xiàn)斐波那契第N項(xiàng)”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)吧2022-06-06

