Stream流 collect() 方法的使用介紹
//1。
Stream 流的注意事項:Stream不呼叫終止方法,中間的操作不會執行。
但是,當我們對 Stream 流中的資料操作完成之後,如果需要將流的結果進行儲存,方便我們接下來對結果的繼續操作,該怎麼辦呢?
Stream 流提供了一個 collect() 方法,可以收集流中的資料到【集合】或者【陣列】中去。
1。收集 Stream 流中的資料到集合中
//1。收集資料到list集合中stream。collect(Collectors。toList())//2。收集資料到set集合中stream。collect(Collectors。toSet())//3。收集資料到指定的集合中Collectors。toCollection(Supplier
示例如下:
/** * 收集Stream流中的資料到集合中 * 備註:切記Stream流只能被消費一次,流就失效了 * 如下只是示例程式碼 */public class CollectDataToCollection{ public static void main(String[] args) { //Stream 流 Stream
測試結果:
[aaa, bbb, ccc, bbb][aaa, ccc, bbb][aaa, bbb, ccc, bbb][aaa, ccc, bbb]
2。收集 Stream 流中的資料到陣列中
//1。使用無參,收集到陣列,返回值為 Object[](Object型別將不好操作)Object[] toArray();//2。使用有參,可以指定將資料收集到指定型別陣列,方便後續對陣列的操作 A[] toArray(IntFunction generator);
示例如下:
/** * 收集Stream流中的資料到陣列中 * 備註:切記Stream流只能被消費一次,流就失效了 * 如下只是示例程式碼 */public class CollectDataToArray{ public static void main(String[] args) { //Stream 流 Stream
測試結果:
data:aaadata:bbbdata:cccdata:bbb————————-data:aaa,length:3data:bbb,length:3data:ccc,length:3data:bbb,length:3
3。Stream流中資料聚合/分組/分割槽/拼接操作
除了 collect() 方法將資料收集到集合/陣列中。對 Stream流 的收集還有其他的方法。比如說:聚合計算,分組,多級分組,分割槽,拼接等。
附:Student實體類(接下來介紹,將根據Student類來進行聚合、分組、分割槽、拼接介紹)
/** * TODO Student實體類 * * @author liuzebiao */public class Student { private String name; private int age; private int score; public Student(String name, int age, int score) { this。name = name; this。age = age; this。score = score; } public String getName() { return name; } public void setName(String name) { this。name = name; } public int getAge() { return age; } public void setAge(int age) { this。age = age; } public int getScore() { return score; } public void setScore(int score) { this。score = score; } @Override public String toString() { return “Student{” + “name=‘” + name + ’\‘’ + “, age=” + age + “, score=” + score + ‘}’; }}
1。聚合操作
當我們使用 Stream 流處理資料後,可以像資料庫的聚合函式一樣對某個欄位進行操作。比如獲取最大值,獲取最小值,求總和,求平均值,統計數量等操作。
//最大值Collectors。maxBy();//最小值Collectors。minBy();//總和Collectors。summingInt();/Collectors。summingDouble();/Collectors。summingLong();//平均值Collectors。averagingInt();/Collectors。averagingDouble();/Collectors。averagingLong();//總個數Collectors。counting();
示例如下:
/** * Stream流資料——聚合操作 * 備註:切記Stream流只能被消費一次,流就失效了 * 如下只是示例程式碼 * @author liuzebiao */public class CollectDataToArray{ public static void main(String[] args) { Stream
測試結果:
最大值:Student{name=‘迪麗熱巴’, age=56, score=99}最小值:Student{name=‘柳巖’, age=52, score=77}年齡總和:222分數平均值:89。75數量為:4
2。分組操作
當我們使用 Stream 流處理資料後,可以根據某個屬性來將資料進行分組。
//接收一個 Function 引數groupingBy(Function<? super T, ? extends K> classifier)
示例如下:
/** * Stream流資料——分組操作 * 備註:切記Stream流只能被消費一次,流就失效了 * 如下只是示例程式碼 * @author liuzebiao */public class CollectDataToArray{ public static void main(String[] args) { Stream
測試結果:
52——>[Student{name=‘趙麗穎’, age=52, score=56}, Student{name=‘柳巖’, age=52, score=53}]56——>[Student{name=‘楊穎’, age=56, score=88}, Student{name=‘迪麗熱巴’, age=56, score=99}]————————————————————————————————————————————————-不及格——>[Student{name=‘趙麗穎’, age=52, score=56}, Student{name=‘柳巖’, age=52, score=53}]及格——>[Student{name=‘楊穎’, age=56, score=88}, Student{name=‘迪麗熱巴’, age=56, score=99}]————————————————————————————————————————————————-52——>Student{name=‘趙麗穎’, age=52, score=95}56——>Student{name=‘楊穎’, age=56, score=88}
3。多級分組操作
當我們使用 Stream 流處理資料後,可以根據某個屬性來將資料進行分組。
//接收兩個引數: 1。Function 引數 2。Collector多級分組groupingBy(Function<? super T, ? extends K> classifier,Collector<? super T, A, D> downstream)
示例如下:
/** * Stream流資料——多級分組操作 * 備註:切記Stream流只能被消費一次,流就失效了 * 如下只是示例程式碼 * @author liuzebiao * @Date 2020-1-10 13:37 */public class CollectDataToArray{ public static void main(String[] args) { Stream
測試結果:
年齡:52 {不及格=[Student{name=‘柳巖’, age=52, score=33}]} {及格=[Student{name=‘趙麗穎’, age=52, score=95}]}年齡:56 {不及格=[Student{name=‘迪麗熱巴’, age=56, score=55}]} {及格=[Student{name=‘楊穎’, age=56, score=88}]}
4。分割槽操作
我們在前面學習了 Stream流中資料的分組操作,我們可以根據屬性完成對資料的分組。接下來我們介紹分割槽操作,我們透過使用
Collectors。partitioningBy()
,根據返回值是否為 true,把集合分為兩個列表,一個 true 列表,一個 false 列表。
分組和分割槽的區別就在:
分組可以有多個組。分割槽只會有兩個區( true 和 false)
//1。一個引數partitioningBy(Predicate<? super T> predicate) //2。兩個引數(多級分割槽)partitioningBy(Predicate<? super T> predicate, Collector<? super T, A, D> downstream)
示例如下:
/** * Stream流資料——多級分組操作 * 備註:切記Stream流只能被消費一次,流就失效了 * 如下只是示例程式碼 * @author liuzebiao */public class CollectDataToArray{ public static void main(String[] args) { Stream
測試結果:
false——>[Student{name=‘迪麗熱巴’, age=56, score=55}, Student{name=‘柳巖’, age=52, score=33}]true——>[Student{name=‘趙麗穎’, age=52, score=95}, Student{name=‘楊穎’, age=56, score=88}]
5。拼接操作
Collectors。joining() 會根據指定的連線符,將所有元素連線成一個字串。
//無引數——等價於 joining(“”);joining()//一個引數joining(CharSequence delimiter)//三個引數(字首+字尾)joining(CharSequence delimiter, CharSequence prefix,CharSequence suffix)
示例如下:
/** * Stream流資料——多級分組操作 * 備註:切記Stream流只能被消費一次,流就失效了 * 如下只是示例程式碼 * @author liuzebiao */public class CollectDataToArray{ public static void main(String[] args) { Stream
測試結果:
趙麗穎楊穎迪麗熱巴柳巖趙麗穎,楊穎,迪麗熱巴,柳巖^_^趙麗穎—楊穎—迪麗熱巴—柳巖>_<