Java Stream流與使用操作指南
一、什么是stream流
Java 8引入的Stream API是處理集合數(shù)據(jù)的一種全新方式,它代表了對(duì)數(shù)據(jù)元素序列進(jìn)行函數(shù)式操作的抽象。Stream不是數(shù)據(jù)結(jié)構(gòu),而是一種高級(jí)的數(shù)據(jù)處理工具,允許你以聲明式的方式處理數(shù)據(jù)集合,類似于SQL語句操作數(shù)據(jù)庫。
Stream的核心特點(diǎn):
- 聲明式編程:只需說明"做什么"而非"如何做",代碼更簡(jiǎn)潔易讀
- 函數(shù)式風(fēng)格:與Lambda表達(dá)式完美結(jié)合,操作更靈活
- 管道操作:多個(gè)操作可以連接起來形成數(shù)據(jù)處理流水線
- 內(nèi)部迭代:不同于集合的外部迭代(使用for-each),Stream在內(nèi)部處理迭代過程
- 延遲執(zhí)行:中間操作不會(huì)立即執(zhí)行,只有遇到終結(jié)操作才會(huì)觸發(fā)計(jì)算
- 不可復(fù)用:一個(gè)Stream只能被消費(fèi)一次
二、創(chuàng)建stream流
1.單列集合創(chuàng)建stream流
// 1.單列集合創(chuàng)建stream流
List<String> list = Arrays.asList("張三", "李四", "王五", "趙六", "錢七");
list.stream().forEach(e -> System.out.println("e = " + e));2.雙列集合創(chuàng)建stream流
// 2.雙列集合創(chuàng)建stream流
HashMap<String, Integer> map = new HashMap<>();
map.put("aaa", 111);
map.put("bbb", 222);
map.put("ccc", 333);
map.put("ddd", 444);
map.put("eee", 555);
// 遍歷所有的key
map.keySet().stream().forEach(e -> System.out.println("e = " + e));
// 遍歷所有的鍵值對(duì)
map.entrySet().stream().forEach(e -> System.out.println("e = " + e));
// 遍歷所有的值
map.values().stream().forEach(e -> System.out.println("e = " + e));3.數(shù)組創(chuàng)建stream流
// 3.數(shù)組創(chuàng)建stream流
int[] array1 = {1, 2, 3};
String[] array2 = {"a", "b", "c"};
Arrays.stream(array1).forEach(e -> System.out.println("e = " + e));4.零散數(shù)據(jù)創(chuàng)建stream流
// 4.零散數(shù)據(jù)創(chuàng)建stream流
Stream.of(1, 2, 3, 4, 5).forEach(e -> System.out.println("e = " + e));
// stream接口中靜態(tài)方法of的細(xì)節(jié)
// 方法的形參是一個(gè)可變參數(shù),可以傳遞一堆零散的數(shù)據(jù)(同一類型),也可以傳遞數(shù)組
// 傳遞的數(shù)組必須是引用數(shù)據(jù)類型的,如果傳遞基本數(shù)據(jù)類型,會(huì)把數(shù)組當(dāng)做一個(gè)元素
Stream.of(array1).forEach(e -> System.out.println("e = " + e)); // 結(jié)果[I@4dd8dc3
Stream.of(array2).forEach(e -> System.out.println("e = " + e));5.創(chuàng)建并行流
// 5.創(chuàng)建并行流
list.parallelStream().forEach(e -> System.out.println("e = " + e)); // 順序會(huì)打亂6.創(chuàng)建無限流
// 6.創(chuàng)建無限流
Stream.generate(Math::random).limit(10).forEach(e -> System.out.println("e = " + e));
Stream.iterate(0, n -> n + 2).limit(10).forEach(e -> System.out.println("e = " + e));三、流的中間操作
中間操作返回一個(gè)新的Stream,可以鏈?zhǔn)秸{(diào)用多個(gè)中間操作。
注意點(diǎn):
一、中間方法,返回新的stream流,原來的stream流只能使用一次,建議使用鏈?zhǔn)骄幊?/p>
二、修改stream流中的數(shù)據(jù),不會(huì)影響原來集合或數(shù)組中的數(shù)據(jù)
1.過濾操作
// 1.過濾操作
// filter: 過濾元素
List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 100, 100);
List<Integer> list2 = Arrays.asList(100, 200, 30, 40, 50, 60, 70, 80, 90, 100, 200, 1000, 100000);
list1.stream().filter(n -> n % 2 == 0).forEach(e -> System.out.println("e = " + e));
// distinct: 去重
// 依賴于hashCode和equals方法,底層使用的是hashset
list1.stream().distinct().forEach(e -> System.out.println("e = " + e));2.映射操作
// 2.映射操作
List<String> list3 = Arrays.asList("張三-13", "李四-14", "王五-15", "趙六-16", "錢七-17");
list3.stream().map(s -> Integer.valueOf(s.split("-")[1])).forEach(e -> System.out.println("map = " + e));
List<List<Integer>> numberLists = Arrays.asList(
Arrays.asList(1, 2),
Arrays.asList(3, 4, 5),
Arrays.asList(6)
);
// flatMap 可以處理嵌套集合或需要"展平"數(shù)據(jù)結(jié)構(gòu)的情況,將每個(gè)元素轉(zhuǎn)換為一個(gè)流(Stream),然后把所有流合并成一個(gè)流
// 當(dāng)你需要處理嵌套結(jié)構(gòu)或一對(duì)多轉(zhuǎn)換時(shí),使用flatMap;當(dāng)只是簡(jiǎn)單的一對(duì)一轉(zhuǎn)換時(shí),使用map
numberLists.stream().map(s -> s.stream()).forEach(stream -> System.out.println("stream = " + stream)); // 輸出3個(gè)流對(duì)象的地址值 java.util.stream.ReferencePipeline$Head@3b9a45b3
numberLists.stream().flatMap(s -> s.stream()).forEach(s -> System.out.println("flatMap = " + s)); // 輸出 1 2 3 4 5 63.截取/跳過操作
// 3.截取/跳過操作
// limit: 限制元素?cái)?shù)量
list1.stream().limit(4).forEach(e -> System.out.println("e = " + e));
// skip: 跳過前N個(gè)元素
list1.stream().skip(5).forEach(e -> System.out.println("e = " + e));4.排序操作
// 4.排序操作
// sorted: 排序
Stream.of("b", "a", "c").sorted().forEach(s -> System.out.println("sorted = " + s));
// 自定義排序
Stream.of("aaa", "bb", "c").sorted(Comparator.comparingInt(String::length)).forEach(s -> System.out.println("customSorted = " + s));
Stream.of("aaa", "bb", "c", null).sorted((a, b) -> {
if (a == null && b == null) return 0;
if (a == null) return 1;
if (b == null) return -1;
return b.compareTo(a);
}).forEach(s -> System.out.println("customSorted2 = " + s));5.合并操作 ??????
// 5.合并操作
Stream.concat(list1.stream(), list2.stream()).forEach(e -> System.out.println("e = " + e));6.其他中間操作
此API可以很好的證明流“延遲執(zhí)行”的特點(diǎn),當(dāng)無forEach()時(shí),不會(huì)打印出中間元素,當(dāng)有終結(jié)操作時(shí),流才會(huì)執(zhí)行。 ??????
// 6.其他中間操作
// peek: 查看流中元素,調(diào)試用
Stream.of("one", "two", "three")
.peek(e -> System.out.println("Original: " + e))
.map(String::toUpperCase)
.peek(e -> System.out.println("Mapped: " + e))
.forEach(e-> System.out.println("peek = " + e));四、 流的終結(jié)操作
終結(jié)操作會(huì)觸發(fā)流的處理并返回結(jié)果
1.遍歷操作 ??????
// 1.遍歷操作
List<String> list = Arrays.asList("張三-13", "李四-14", "王五-15", "趙六-16", "錢七-17", "張三-23");
// forEach: 遍歷元素
list.stream().forEach(e -> System.out.println("e = " + e));
// forEachOrdered: 保證順序的遍歷
list.parallelStream().forEachOrdered(e -> System.out.println("e = " + e));2.收集操作 ??????
// 2.收集操作
// 收集到list集合
List<String> nameList = list.stream().map(e -> e.split("-")[0]).collect(Collectors.toList());
System.out.println("nameList = " + nameList);
// 收集到set集合,會(huì)去重
Set<String> nameSet = list.stream().map(e -> e.split("-")[0]).collect(Collectors.toSet());
System.out.println("nameSet = " + nameSet);
// 收集到map集合
// 注意收集到map,鍵不能重復(fù),重復(fù)會(huì)報(bào) java.lang.IllegalStateException: Duplicate key 張三 (attempted merging values 13 and 23)
// Map<String, Integer> map1 = list.stream().collect(Collectors.toMap(s -> s.split("-")[0], s -> Integer.valueOf(s.split("-")[1])));
// System.out.println("map1 = " + map1);
// 解決方案,傳遞第三個(gè)參數(shù),如果鍵重復(fù),使用前面的那么,以下的(a, b)代表的value,a為已經(jīng)存在的,b為新加的
Map<String, Integer> map2 = list.stream().collect(Collectors.toMap(s -> s.split("-")[0], s -> Integer.valueOf(s.split("-")[1]), (a, b) -> a));
System.out.println("map2 = " + map2);
// 收集為字符串
String nameStr = list.stream().map(e -> e.split("-")[0]).collect(Collectors.joining("-"));
System.out.println("nameStr = " + nameStr); // 張三-李四-王五-趙六-錢七-張三3.聚合操作
// 3.聚合操作
// count: 計(jì)數(shù)
long count = Stream.of("a", "b", "c").count();
System.out.println("count = " + count); // 3
// max/min: 最大最小值
Optional<String> max = Stream.of("a", "bb", "ccc").max(Comparator.comparingInt(String::length));
System.out.println("max = " + max.get());
// reduce: 歸約操作
// 無初始值,當(dāng)流為空,則為Optional.empty()
Optional<Integer> sum = Stream.of(1, 2, 3).reduce(Integer::sum);
System.out.println("sum = " + sum.get()); // ccc
// 初始值為0,流為空則返回0,執(zhí)行的操作為 0+1+2+3 = 6
Integer sumWithIdentity = Stream.of(1, 2, 3).reduce(0, Integer::sum);
System.out.println("sumWithIdentity = " + sumWithIdentity); // 64.匹配操作 ?????
// 4.匹配操作
// anyMatch: 任意元素匹配(任意一個(gè)滿足條件則為true)
boolean hasEven = Stream.of(1, 2, 3).anyMatch(n -> n % 2 == 0);
System.out.println("hasEven = " + hasEven); // true
// allMatch: 所有元素匹配(所有元素都要滿足條件)
boolean allEven = Stream.of(2, 4, 6).allMatch(n -> n % 2 == 0);
System.out.println("allEven = " + allEven); // true
// noneMatch: 沒有元素匹配(所有元素都不能滿足條件)
boolean noneNegative = Stream.of(1, 2, 3).noneMatch(n -> n < 0);
System.out.println("noneNegative = " + noneNegative); // true5.查找操作 ??????
// 5.查找操作
// findFirst: 查找第一個(gè)元素
Optional<String> first = Stream.of("a", "b", "c").findFirst();
System.out.println("first = " + first.get()); // a
// findAny: 查找任意元素(在并行流中效率更高)
Optional<String> any = Stream.of("a", "b", "c").parallel().findAny();
System.out.println("any = " + any.get()); // b6.數(shù)組轉(zhuǎn)換
// 6.數(shù)組轉(zhuǎn)換
// toArray: 轉(zhuǎn)換為數(shù)組
String[] array = Stream.of("a", "b", "c").toArray(String[]::new);
System.out.println("array = " + Arrays.toString(array)); // [a, b, c]7.分組
// 7.分組
// 可將一個(gè)對(duì)象集合按照里面的某個(gè)字段轉(zhuǎn)換為map,如key為ID,value為對(duì)象
List<String> words = Arrays.asList("apple", "banana", "orange", "pear", "grape");
Map<Integer, List<String>> groupedByLength = words.stream()
.collect(Collectors.groupingBy(String::length));
System.out.println(groupedByLength);到此這篇關(guān)于Java Stream流與使用操作指南的文章就介紹到這了,更多相關(guān)Java Stream流內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java 數(shù)據(jù)庫時(shí)間返回前端顯示錯(cuò)誤(差8個(gè)小時(shí))的解決方法
本文主要介紹了Java 數(shù)據(jù)庫時(shí)間返回前端顯示錯(cuò)誤(差8個(gè)小時(shí))的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08
SpringBoot中的多RabbitMQ數(shù)據(jù)源配置實(shí)現(xiàn)
本篇博客將介紹如何在 Spring Boot 中配置和管理多個(gè) RabbitMQ 數(shù)據(jù)源,以滿足不同的應(yīng)用需求,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
Java使用JavaMail API發(fā)送和接收郵件的代碼示例
JavaMail是Oracle甲骨文開發(fā)的Java郵件類API,支持多種郵件協(xié)議,這里我們就來看一下Java使用JavaMail API發(fā)送和接收郵件的代碼示例2016-06-06
JavaWeb三大組件之監(jiān)聽器Listener詳解
這篇文章主要介紹了JavaWeb三大組件之監(jiān)聽器Listener詳解,在JavaWeb應(yīng)用程序中,Listener監(jiān)聽器是一種機(jī)制,用于監(jiān)聽和響應(yīng)特定的事件,它可以感知并響應(yīng)與應(yīng)用程序相關(guān)的事件,從而執(zhí)行相應(yīng)的邏輯處理,需要的朋友可以參考下2023-10-10
測(cè)試環(huán)境頻繁Full GC問題的解決思路分析
全文介紹了作者通過與調(diào)用方交互,發(fā)現(xiàn)welink-front服務(wù)不可用的問題,通過jmap-heap和jstat-gccause命令,作者找到了問題的原因是元數(shù)據(jù)區(qū)內(nèi)存使用率過高,觸發(fā)了FullGC,作者通過分析GC日志和堆內(nèi)存使用情況,確定了問題的根本原因2025-01-01
Maven 項(xiàng)目生成jar運(yùn)行時(shí)提示“沒有主清單屬性”
這篇文章主要介紹了Maven 項(xiàng)目生成jar運(yùn)行時(shí)提示“沒有主清單屬性”,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
java實(shí)現(xiàn)上傳文件到oss(阿里云)功能示例
這篇文章主要介紹了java實(shí)現(xiàn)上傳文件到oss(阿里云)功能,結(jié)合實(shí)例形式詳細(xì)分析了java上傳文件到阿里云的具體步驟、配置及相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-11-11
SpringMVC如何正確接收時(shí)間的請(qǐng)求示例分析
這篇文章主要為大家介紹了SpringMVC如何正確接收時(shí)間的請(qǐng)求示例分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09

