● 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{ 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 ArrayListgetData() { ArrayList 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) { Streamstream = 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) { Streamstream = 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) { Streamstream = Data.getData().stream(); //summingInt:把流中的元素映射成int类型的元素,求和 System.out.println(Data.getData().stream().collect(Collectors.summingInt(Person::getScore))); }
averagingInt:把流中的元素映射成int类型的元素,求平均值
public static void main(String[] args) { Streamstream = 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) { Streamstream = 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工具类操作的文章就介绍到这了,更多相关Java流式操作Collectors工具类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
上一篇:北京同仁堂有什么好处(北京同仁堂有什么区别) 环球报资讯
下一篇:最后一页
记者从河南省教育考试院了解到,鉴于目前疫情防控严峻形势,为维护广大考生健康安全,经研究并报教育部有关部门批准,河南省原定于6月11日
尖嘴猴腮是人们对猴子的固有认识,但凡事都有例外,最近,郑州市动物园就有一只猴因长着一张方方正正的脸,被送外号人脸猴,其独特的长相令
中原区民政局提醒:尽量避免扎堆,只要感情好,每一天都是好日子5月20日因谐音我爱你,成为很多新人眼中寓意美好的好日子。为满足准新人们
5月19日是中国旅游日,今年中国旅游日的活动主题为感悟中华文化享受美好旅程。记者今日从省、市文旅部门获悉,为进一步提振行业发展信心,
浊波浩浩东倾,今来古往无终极。在中国所有的大江大河中,无论从地理空间还是文化意义上来说,黄河都是一个独特的存在。如果从高空中俯瞰,
在第46个国际博物馆日当天,国家十三五重点文化工程、位于河南洛阳的隋唐大运河文化博物馆,正式面向公众开放。隋唐大运河始建于公元605年
想要了解郑州这片沃土流淌着的文化脉络吗?想要寻找郑州是最早中国历史起源的佐证吗?5月18日国际博物馆日,由郑州市委网信办指导,新浪河
5月18日0—24时,吉林省新增本地确诊病例6例(轻型),其中延边州3例,白山市3例(含2例无症状感染者转为确诊病例);新增本地无症状感染者
2022年5月18日0—24时,天津市新增7例本土新冠肺炎确诊病例(其中4例为无症状感染者转为确诊病例)。新增1例境外输入性新冠肺炎确诊病例。