Java關(guān)于JDK1.8新特性的Stream流
Java 的Stream流
一、定義
JDK1.8 中增加了Stream流,Stream流是一個來自數(shù)據(jù)源的元素隊列并支持聚合操作。元素是特定類型的對象,形成一個隊列,Java中的Stream并不會存儲元素,而是按需計算數(shù)據(jù)源是流的來源,可以使集合,數(shù)組,I/O channel,生成器generator等。聚合操作類似SQL語句一樣的操作,比如filter
,map
,reduce
,match
,sorted
等
二、操作的特征
- 中間的操作都會返回流對象本身,這樣多個操作可以串聯(lián)成一個管道,如同流式風(fēng)格
- 內(nèi)部迭代:以前對集合的遍歷是通過Iterator或者For-Each的方式,顯式的在集合外部進(jìn)行迭代,這叫做外部迭代,而Stream流式內(nèi)部迭代。
三、代碼示例
1、生成流
如下代碼的意思是,首先創(chuàng)建一個集合,然后過濾出所有不為空的元素,重新組成一個集合
List<String> stringList = Arrays.asList("abc", "", "bc", "efg", "abcd", ""); List<String> strings = stringList.stream().filter(string -> StringUtils.isNotBlank(string)).collect(Collectors.toList()); System.out.println("*********過濾后的集合是"+strings);
2、forEach 迭代
strings.forEach(System.out::println);
3、limit方法用于獲取指定數(shù)量的流
如下就是獲取一個長度為10的流。
Random random = new Random(); random.ints().limit(10).forEach(System.out::println);
4、map
map方法用于映射每個元素,以下代碼片段使用map輸出元素對應(yīng)的平方數(shù)
vList<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5, 8, 9); //獲取對應(yīng)的平方數(shù) List<Integer> integers = numbers.stream().map(i -> i * i).distinct().collect(Collectors.toList()); System.out.println("*********獲取對應(yīng)的平方數(shù)"+integers);
5、sorted
sorted方法用于對流進(jìn)行排序
如下,就是按照元素的大小進(jìn)行排序
List<Integer> orgList = Arrays.asList(10, 9, 8, 7, 6, 13, 16, 0, 1, 2); List<Integer> sortedList = orgList.stream().sorted().collect(Collectors.toList()); System.out.println("*********排序后集合是"+sortedList);
6、并行(parallel)程序
List<String> emptyList = Arrays.asList("abc", "", "bc", "efg", "abcd", "", ""); //獲取空字符串的數(shù)量 long count = emptyList.parallelStream().filter(string -> string.isEmpty()).count(); System.out.println("*****集合中的空字符串個數(shù)=" + count);
7、Collectors
Collectors類實現(xiàn)了很多歸約操作,例如將流轉(zhuǎn)換成集合和集合元素,Collectors可用于返回列表或字符串
List<String> orgCollectors = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl"); List<String> filtered = orgCollectors.stream().filter(string -> !string.isEmpty()) .collect(Collectors.toList()); System.out.println("篩選列表:" + filtered); String mergedString = stringList.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(",")); System.out.println("合并字符串:" + mergedString);
統(tǒng)計:
//統(tǒng)計 List<Integer> orgNums = Arrays.asList(3, 2, 2, 3, 7, 3, 5); IntSummaryStatistics statistics = numbers.stream().mapToInt((x) -> x).summaryStatistics(); System.out.println("列表中最大的數(shù):" + statistics.getMax()); System.out.println("列表中最小的數(shù):" + statistics.getMin()); System.out.println("所有數(shù)之和:" + statistics.getSum()); System.out.println("平均數(shù):" + statistics.getAverage());
8、轉(zhuǎn)化(將枚舉類轉(zhuǎn)成map)
將一個枚舉類轉(zhuǎn)成一個map,使用的示例可以參考下方:
Map<Integer, String> disableFlagMap = Arrays.stream(DisableFlagEnum.values()).collect(Collectors.toMap(p -> p.getCode(), p -> p.getDesc()));
完整的代碼是:
public enum DisableFlagEnum { NORMAL(0, "正常"), DELETE(1, "已刪除"); private Integer code; private String desc; DisableFlagEnum(Integer code, String desc) { this.code = code; this.desc = desc; } public Integer getCode() { return code; } public String getDesc() { return desc; } public static String getDesc(Integer code) { Map<Integer, String> disableFlagMap = Arrays.stream(DisableFlagEnum.values()).collect(Collectors.toMap(p -> p.getCode(), p -> p.getDesc())); return disableFlagMap.get(code); } }
demo的運(yùn)行結(jié)果:
總結(jié):
本文首先介紹了JDK1.8中的Stream流,Stream流是是一個來自數(shù)據(jù)源的元素隊列并支持聚合操作。它的特點(diǎn)就是中間過程不會存儲數(shù)據(jù),不會改變數(shù)據(jù)源本身。接著介紹了stream的使用示例,stream流的使用一般是要結(jié)合函數(shù)式接口使用的。
以上就是Java的Stream流的詳細(xì)內(nèi)容,更多關(guān)于Java的Stream流的資料請關(guān)注腳本之家其它相關(guān)文章!希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot設(shè)置默認(rèn)主頁的方法步驟
這篇文章主要介紹了SpringBoot設(shè)置默認(rèn)主頁的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12springboot接口多實現(xiàn)類選擇性注入解決方案
這篇文章主要為大家介紹了springboot接口多實現(xiàn)類選擇性注入解決方案的四種方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03SpringBoot 多線程事務(wù)回滾的實現(xiàn)
本文是基于springboot的@Async注解開啟多線程,并通過自定義注解和AOP實現(xiàn)的多線程事務(wù),避免繁瑣的手動提交/回滾事務(wù),感興趣的可以了解一下2024-02-02Spring Security 表單登錄功能的實現(xiàn)方法
這篇文章主要介紹了Spring Security 表單登錄,本文將構(gòu)建在之前簡單的 Spring MVC示例 之上,因為這是設(shè)置Web應(yīng)用程序和登錄機(jī)制的必不可少的。需要的朋友可以參考下2019-06-06