Java中Stream流的常用方法代碼示例
stream流簡介
stream流操作是Java 8提供一個重要新特性,它允許開發(fā)人員以聲明性方式處理集合,其核心類庫主要改進(jìn)了對集合類的 API和新增Stream操作。
Stream類中每一個方法都對應(yīng)集合上的一種操作。將真正的函數(shù)式編程引入到Java中,能 讓代碼更加簡潔,極大地簡化了集合的處理操作,提高了開發(fā)的效率和生產(chǎn)力。
基本數(shù)據(jù)
自定義實體
@Data
class Student{
private String name;
private Integer age;
private Double height;
public Student() {
}
}
假數(shù)據(jù)
Student s1 = new Student();
s1.setAge(20);
s1.setName("cookie");
s1.setHeight(180d);
Student s2 = new Student();
s2.setAge(30);
s2.setName("cookie");
s2.setHeight(180d);
Student s3 = new Student();
s3.setAge(40);
s3.setName("bob");
s3.setHeight(175d);
Student s4 = new Student();
s4.setAge(40);
s4.setName("bob");
s4.setHeight(180d);
// 存入list集合
List<Student> list = new ArrayList<>();
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
一、分組
1. 一層分組/簡單分組
/**
* 需求一(一層分組):根據(jù)Age分組
*/
System.out.println("需求一(一層分組):根據(jù)Age分組");
Map<Integer, List<Student>> collect = list.stream().collect(Collectors.groupingBy(Student::getAge));
for (Integer age : collect.keySet()) {
System.out.println("key:" + age + "\tvalue:" + collect.get(age));
}
/**
* 控制臺結(jié)果:
* key:20 value:[Student(name=cookie, age=20, height=180.0)]
* key:40 value:[Student(name=bob, age=40, height=175.0), Student(name=bob, age=40, height=180.0)]
* key:30 value:[Student(name=cookie, age=30, height=180.0)]
*/
2. 多層分組
/**
* 需求二: 先根據(jù)name分組,然后再根據(jù)身高分組
*/
System.out.println("需求二: 先根據(jù)name分組,然后再根據(jù)身高分組");
Map<String, Map<Double, List<Student>>> collect1 = list.stream()
.collect(Collectors.groupingBy(Student::getName, Collectors.groupingBy(Student::getHeight)));
Set<String> namesGroup = collect1.keySet();
for (String namekey : namesGroup) {
Map<Double, List<Student>> heightGroupMap = collect1.get(namekey);
Set<Double> height = heightGroupMap.keySet();
for (Double h : height) {
System.out.println("name:" + namekey + " height:" + heightGroupMap.get(h));
}
}
/**
* 控制臺結(jié)果:
* name:bob height:[Student(name=bob, age=40, height=175.0)]
* name:bob height:[Student(name=bob, age=40, height=180.0)]
* name:cookie height:[Student(name=cookie, age=20, height=180.0), Student(name=cookie, age=30, height=180.0)]
*/
3. 多層分組-自定義key
/**
* 需求三: 自定義key返回 形式如下: age_height bob_175
*/
System.out.println("需求三: 自定義key返回 形式如下: age_height bob_175");
Map<String, List<Student>> collect2 = list.stream()
.collect(Collectors.groupingBy(c -> c.getName() + "_" + c.getHeight()));
for (String customKey : collect2.keySet()) {
System.out.println("key:" + customKey +" value:"+ collect2.get(customKey));
}
/**
* 控制臺結(jié)果:
* key:bob_180.0 value:[Student(name=bob, age=40, height=180.0)]
* key:bob_175.0 value:[Student(name=bob, age=40, height=175.0)]
* key:cookie_180.0 value:[Student(name=cookie, age=20, height=180.0), Student(name=cookie, age=30, height=180.0)]
*/
二、排序
方式一: 通過自定義的比較器(非必要不推薦)
/**
* 需求: 根據(jù)身高排序,如果身高相同,根據(jù)年齡排序,如果年齡依然相同,根據(jù)名稱字母順序排序
*/
List<Student> collect3 = list.stream().sorted(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
// 這里前面的減去后面的是升序, 反之這是降序
if (!o1.getHeight().equals(o2.getHeight())) {
return (int) (o1.getHeight() - o2.getHeight());
}
if (!o1.getAge().equals(o2.getAge())) {
return o1.getAge() - o2.getAge();
}
return o1.getName().compareTo(o2.getName());
}
}).collect(Collectors.toList());
System.out.println(collect3);
/**
* 控制臺結(jié)果:
* [Student(name=bob, age=40, height=175.0),
* Student(name=cookie, age=20, height=180.0),
* Student(name=cookie, age=30, height=180.0),
* Student(name=bob, age=40, height=180.0)]
*/
// 注: 當(dāng)然上面的也可以做一個簡化
List<Student> collect3 = list.stream().sorted((o1, o2) -> {
// 這里前面的減去后面的是升序, 反之這是降序
if (!o1.getHeight().equals(o2.getHeight())) {
return (int) (o1.getHeight() - o2.getHeight());
}
if (!o1.getAge().equals(o2.getAge())) {
return o1.getAge() - o2.getAge();
}
return o1.getName().compareTo(o2.getName());
}).collect(Collectors.toList());
方式二: 通過lambda
List<Student> collect4 = list.stream()
.sorted(Comparator.comparingDouble(Student::getHeight)
.thenComparingInt(Student::getAge)
.thenComparing(Student::getName))
.collect(Collectors.toList());
System.out.println(collect4);
/**
* 控制臺結(jié)果:
* [Student(name=bob, age=40, height=175.0),
* Student(name=cookie, age=20, height=180.0),
* Student(name=cookie, age=30, height=180.0),
* Student(name=bob, age=40, height=180.0)]
*/
// 注意:
// 方式一,升序降序是通過返回的正負(fù),
// 方式二而是通過方法, 現(xiàn)在我們首先通過身高降序, 我們只需要在條件的后面加一個reversed()后綴方法即可
List<Student> collect4 = list.stream().sorted(Comparator.comparingDouble(Student::getHeight).reversed()
.thenComparingInt(Student::getAge)
.thenComparing(Student::getName)
).collect(Collectors.toList());
System.out.println(collect4);
/**
* 修改之后控制臺結(jié)果:
* [Student(name=cookie, age=20, height=180.0),
* Student(name=cookie, age=30, height=180.0),
* Student(name=bob, age=40, height=180.0),
* Student(name=bob, age=40, height=175.0)]
*/
三、 統(tǒng)計
/** * 需求: 統(tǒng)計年齡之和 */ int ageSum = list.stream().mapToInt(Student::getAge).sum(); /** * 求年齡平均值 */ Double ageAvg1 = list.stream().collect(Collectors.averagingInt(Student::getAge)); // 或者 double ageAvg2 = list.stream().mapToInt(Student::getAge).average().getAsDouble(); /** * 求年齡最大值 */ int maxAge = list.stream().mapToInt(Student::getAge).max().getAsInt(); /** * 最小值 */ int minAge = list.stream().mapToInt(Student::getAge).min().getAsInt();
到此這篇關(guān)于Java中Stream流的常用方法代碼示例的文章就介紹到這了,更多相關(guān)Stream流的常用方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java強(qiáng)制保留兩位小數(shù)的四種方法案例詳解
這篇文章主要介紹了Java強(qiáng)制保留兩位小數(shù)的四種方法案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
IDEA設(shè)置字體隨鼠標(biāo)滾動放大縮小的實現(xiàn)
這篇文章主要介紹了IDEA設(shè)置字體隨鼠標(biāo)滾動放大縮小的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
SpringBoot+Vue前后端分離實現(xiàn)請求api跨域問題
這篇文章主要介紹了SpringBoot+Vue前后端分離實現(xiàn)請求api跨域問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06

