Java8Stream

目錄

什么是Stream?

IO流:

Java8Stream:

什么是流?

stream圖解

獲取流

集合類,使用 Collection 接口下的 stream()

代碼

數組類,使用 Arrays 中的 stream() 方法

代碼

stream,使用 Stream 中的靜態方法

代碼

流操作

按步驟寫

代碼

鏈式調用

代碼

運行

中間操作:

?API

代碼?

運行

代碼?

運行

代碼

運行?

終端操作:

API

代碼

運行

代碼

運行

代碼

運行

代碼

運行

代碼

運行

代碼

運行


什么是Stream?

IO流:

輸入輸出文件的

Java8Stream:

處理數據集合(數組,集合類);

對數組,集合類 進行各種操作(過濾,排序......);

stream處理數據的大體過程:

數組/集合類-->流-->各種操作(排序,過濾...)-->結果。

什么是流?

數據和集合類更偏向于存儲數據(各種結構);

stream更偏向于數據操作。

stream圖解

獲取流

獲取流,把集合或者數組轉化為stream對象。

集合類,使用 Collection 接口下的 stream()
代碼
package com.ffyc.stream;import java.util.ArrayList; 
import java.util.stream.Stream;public class Demo1 {public static void main(String[] args) { ArrayList<Integer> arrayList = new ArrayList<>();arrayList.add(1);arrayList.add(2);arrayList.add(3);arrayList.add(4);//把集合轉為流Stream<Integer> stream = arrayList.stream(); }
}
數組類,使用 Arrays 中的 stream() 方法
代碼
package com.ffyc.stream; import java.util.Arrays;
import java.util.stream.IntStream; public class Demo1 {public static void main(String[] args) { int[] array = new int[]{1,2,3,4};//把數組轉為流IntStream intStream = Arrays.stream(array); }
}
stream,使用 Stream 中的靜態方法
代碼
package com.ffyc.stream;import java.util.stream.Stream;public class Demo1 {public static void main(String[] args) { Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5);}
}

流操作

按步驟寫
代碼
package com.ffyc.stream;import java.util.Arrays;
import java.util.stream.Stream;public class Demo2 {public static void main(String[] args) {   Integer[] array = new Integer[]{1,2,3,4,5};Stream<Integer> stream = Arrays.stream(array);Stream stream1 = stream.filter();Stream stream2 = stream1.sorted();}
}
鏈式調用
代碼
package com.ffyc.stream;import java.util.Arrays;
import java.util.stream.Stream;public class Demo2 {public static void main(String[] args) { long sum = Arrays.stream(array).sorted().count();}
}
運行

中間操作:

流的各種數據處理

?API

filter:過濾流中的某些元素,
sorted(): 自然排序,流中元素需實現 Comparable 接口
distinct: 去除重復元素?

代碼?
package com.ffyc.stream;import java.util.Arrays; public class Demo2 {public static void main(String[] args) { Integer[] array = new Integer[]{1,2,3,4,2,5}; Arrays.stream(array).filter((e)->{return e<5;}).sorted((o1,o2)->{return o2-o1;}).distinct().forEach((e)->{System.out.println(e);});}
}
運行

limit(n): 獲取 n 個元素
skip(n): 跳過 n 元素,配合 limit(n)可實現分頁

代碼?
package com.ffyc.stream;import java.util.Arrays; public class Demo2 {public static void main(String[] args) { Integer[] array = new Integer[]{1,2,3,4,2,5}; Arrays.stream(array)//跳過指定數量個元素.skip(2)//取出指定數量個元素.limit(2).forEach((e)->{System.out.println(e);}); }
}
運行

map():將其映射成一個新的元素

代碼
package com.ffyc.stream;public class Student {private int num;private String name;private int age;public Student(int num, String name, int age) {this.num = num;this.name = name;this.age = age;}public int getNum() {return num;}public void setNum(int num) {this.num = num;}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;}@Overridepublic String toString() {return "Student{" +"num=" + num +", name='" + name + '\'' +", age=" + age +'}';}
}
package com.ffyc.stream;import java.util.ArrayList;
import java.util.Arrays; 
import java.util.Map;
import java.util.stream.Collectors;public class Demo4 {public static void main(String[] args) {Student s1 = new Student(001,"張三",18);Student s2 = new Student(002,"李四",19);Student s3 = new Student(003,"王五",29);Student s4 = new Student(004,"王麻子",21);Student s5 = new Student(005,"麗麗",19);ArrayList<Student> students = new ArrayList<>();students.add(s3);students.add(s1);students.add(s2);students.add(s5);students.add(s4); Object[] array = students.stream().map((Student::getNum))//將對象中某個屬性的值映射到一個新集合中.toArray();System.out.println(Arrays.toString(array));Map<Integer, String> map = students.stream().collect(Collectors.toMap(Student::getNum, Student::getName));System.out.println(map);}
}
運行?

終端操作:

把流轉為最終結果(數組/集合/單值)

API

Min:返回流中元素最小值
Max:返回流中元素最大值?

代碼
package com.ffyc.stream;import java.util.Arrays; public class Demo3 {public static void main(String[] args) { Integer[] array = new Integer[]{1,2,3,4,2,5};Integer max = Arrays.stream(array).distinct().max((o1, o2) -> {//最大值return o1 - o2;}).get();System.out.println(max);Integer min = Arrays.stream(array).distinct().min((o1, o2) -> {//最小值return o1 - o2;}).get();System.out.println(min); 
}
運行

count:返回流中元素的總個數

代碼
package com.ffyc.stream;import java.util.Arrays; public class Demo3 {public static void main(String[] args) { Integer[] array = new Integer[]{1,2,3,4,2,5}; long count = Arrays.stream(array).distinct().count();//統計元素個數System.out.println(count); }
}
運行

Reduce:所有元素求和

代碼
package com.ffyc.stream;import java.util.Arrays; public class Demo3 {public static void main(String[] args) { Integer[] array = new Integer[]{1,2,3,4,2,5}; /long reduce = Arrays.stream(array).distinct().reduce((a,b)->{//求和return a+b;}).get();System.out.println(reduce); }
}
運行

anyMatch:接收一個 Predicate 函數,只要流中有一個元素滿足條件則返回 true,否則返回 false
allMatch:接收一個 Predicate 函數,當流中每個元素都符合條件時才返回 true,否則返回 false

代碼
package com.ffyc.stream;import java.util.Arrays; public class Demo3 {public static void main(String[] args) { Integer[] array = new Integer[]{1,2,3,4,2,5}; Boolean result1 = Arrays.stream(array).distinct().anyMatch((e)->{//只有有一個元素滿足條件,返回truereturn e>2;});System.out.println(result1);Boolean result2 = Arrays.stream(array).distinct().allMatch((e)->{//所有的條件都滿足條件,返回truereturn e>2;});System.out.println(result2); }
}
運行

findFirst:返回流中第一個元素

代碼
package com.ffyc.stream;import java.util.Arrays; public class Demo3 {public static void main(String[] args) { Integer[] array = new Integer[]{1,2,3,4,2,5}; Integer result = Arrays.stream(array).distinct().findFirst().get();System.out.println(result);}
}
運行

collect:將流中的元素倒入一個集合,Collection 或 Map

代碼
package com.ffyc.stream;public class Student {private int num;private String name;private int age;public Student(int num, String name, int age) {this.num = num;this.name = name;this.age = age;}public int getNum() {return num;}public void setNum(int num) {this.num = num;}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;}@Overridepublic String toString() {return "Student{" +"num=" + num +", name='" + name + '\'' +", age=" + age +'}';}
}
package com.ffyc.stream;import java.util.ArrayList; 
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;public class Demo4 {public static void main(String[] args) {Student s1 = new Student(001,"張三",18);Student s2 = new Student(002,"李四",19);Student s3 = new Student(003,"王五",29);Student s4 = new Student(004,"王麻子",21);Student s5 = new Student(005,"麗麗",19);ArrayList<Student> students = new ArrayList<>();students.add(s3);students.add(s1);students.add(s2);students.add(s5);students.add(s4);List<Student> collect = students.stream().sorted((a,b)->{ //對學生對象集合進行排序,必須給定排序的規則return a.getNum()-b.getNum();}).collect(Collectors.toList());System.out.println(collect);List<Student> res = students.stream().filter((stu) -> {return  stu.getAge()>18;}).collect(Collectors.toList());System.out.println(res);List<Integer> list = students.stream().map(Student::getNum).collect(Collectors.toList());System.out.println(list);}
}
運行

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/16268.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/16268.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/16268.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

重生之 SpringBoot3 入門保姆級學習(02、打包部署)

重生之 SpringBoot3 入門保姆級學習&#xff08;02、打包部署&#xff09; 1.6 打包插件1.7 測試 jar 包1.8 application.properties 的相關配置 1.6 打包插件 官網鏈接 https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started.html#getting-starte…

【Python】 XGBoost模型的使用案例及原理解析

原諒把你帶走的雨天 在漸漸模糊的窗前 每個人最后都要說再見 原諒被你帶走的永遠 微笑著容易過一天 也許是我已經 老了一點 那些日子你會不會舍不得 思念就像關不緊的門 空氣里有幸福的灰塵 否則為何閉上眼睛的時候 又全都想起了 誰都別說 讓我一個人躲一躲 你的承諾 我竟然沒懷…

自學動態規劃—— 一和零

一和零 474. 一和零 - 力扣&#xff08;LeetCode&#xff09; 其實遇到這種還好說&#xff0c;我寧愿遇見這種&#xff0c;也不想遇見那些奇奇怪怪遞推公式的題目。 這里其實相當背包要滿足兩個條件&#xff0c;所以我們可以將dp開成二維的&#xff0c;之后的操作&#xff0…

Kubernetes(K8S) 集群環境搭建指南

Kubernetes&#xff08;簡稱K8s&#xff09;是一個開源的容器編排平臺&#xff0c;旨在自動化部署、擴展和管理容器化應用。K8S環境搭建過程比較復雜&#xff0c;涉及到非常多組件安裝和系統配置&#xff0c;本文將會詳細介紹如何在服務器上搭建好Kubernetes集群環境。 在學習…

C語言---求一個整數存儲在內存中的二進制中1的個數--3種方法

//編寫代碼實現&#xff1a;求一個整數存儲在內存中的二進制中1的個數 //第一種寫法 /*int count_bit_one(unsigned int n) {int count 0;while (n )//除到最后余數是0&#xff0c;那么這個循環就結束了{//這個題就是可以想成求15的二進制的過程//每次都除以2&#xff0c;余數…

跟小伙伴們說一下

因為很忙&#xff0c;有一段時間沒有更新了&#xff0c;這次先把菜鳥教程停更一下&#xff0c;因為自己要查缺補漏一些細節問題&#xff0c;而且為了方便大家0基礎也想學C語言&#xff0c;這里打算給大家開一個免費專欄&#xff0c;這里大家就可以好好學習啦&#xff0c;哪怕0基…

面試題·棧和隊列的相互實現·詳解

A. 用隊列實現棧 用隊列實現棧 實現代碼如下 看著是隊列&#xff0c;其實實際實現更接近數組模擬 typedef struct {int* queue1; // 第一個隊列int* queue2; // 第二個隊列int size; // 棧的大小int front1, rear1, front2, rear2; // 兩個隊列的首尾指針 } MyS…

圖像處理ASIC設計方法 筆記25 紅外成像技術:未來視覺的革命

在當今科技飛速發展的時代,紅外成像技術以其獨特的優勢,在醫療、工業檢測等多個領域扮演著越來越重要的角色。本章節(P146 第7章紅外焦平面非均勻性校正SoC)將深入探討紅外成像系統中的關鍵技術——非均勻性校正SoC,以及它如何推動紅外成像技術邁向新的高度。 紅外成像系統…

6.Redis之String命令

1.String類型基本介紹 redis 所有的 key 都是字符串, value 的類型是存在差異的~~ 一般來說,redis 遇到亂碼問題的概率更小~~ Redis 中的字符串,直接就是按照二進制數據的方式存儲的. (不會做任何的編碼轉換【講 mysql 的時候,知道 mysql 默認的字符集, 是拉丁文,插入中文…

Jenkins--從入門到入土

Jenkins–從入門到入土 文章目錄 Jenkins--從入門到入土〇、概念提要--什么是CI/DI&#xff1f;1、CI&#xff08;Continuous Integration&#xff0c;持續集成&#xff09;2、DI&#xff08;DevOps Integration&#xff0c;DevOps 集成&#xff09;3、解決的問題 一、Jenkins安…

iOS 開發系列:基于VNRecognizeTextRequest識別圖片文字

1.添加Vision Kit依賴 在項目設置中點擊"General"選項卡&#xff0c;然后在"Frameworks, Libraries, and Embedded Content"&#xff08;框架、庫和嵌入內容&#xff09;部分&#xff0c;點擊""按鈕。搜索并選擇"Vision.framework"。…

[AIGC] flink sql 消費kafka消息,然后寫到mysql中的demo

這是一個使用 Flink SQL 從 Kafka 中消費數據并寫入 MySQL 的示例。在這個示例中&#xff0c;我們將假設有一個 Kafka 主題 “input_topic”&#xff0c;它產生格式為 (user_id: int, item_id: int, behavior: string, timestamp: long) 的數據&#xff0c;我們需要把這些數據寫…

world machine學習筆記(4)

選擇設備&#xff1a; select acpect&#xff1a; heading&#xff1a;太陽的方向 elevation&#xff1a;太陽的高度 select colour&#xff1a;選擇顏色 select convexity&#xff1a;選擇突起&#xff08;曲率&#xff09; select height&#xff1a;選擇高度 falloff&a…

用常識滾雪球:拼多多的內生價值,九年的變與不變

2024年5月22日&#xff0c;拼多多公布了今年一季度財報&#xff0c;該季度拼多多集團營收868.1億元&#xff0c;同比增長131%&#xff0c;利潤306.0億&#xff0c;同比增長了202%&#xff0c;數據亮眼。 市場對拼多多經歷了“看不見”、“看不懂”、“跟不上”三個階段。拼多多…

Vue.js條件渲染與列表渲染指南

title: Vue.js條件渲染與列表渲染指南 date: 2024/5/26 20:11:49 updated: 2024/5/26 20:11:49 categories: 前端開發 tags: VueJS前端開發數據綁定列表渲染狀態管理路由配置性能優化 第1章&#xff1a;Vue.js基礎與環境設置 1.1 Vue.js簡介 Vue.js (讀音&#xff1a;/vju…

SwiftUI中的Slider的基本使用

在SwiftUI中&#xff0c;可以使用Slider視圖創建一個滑動條&#xff0c;允許用戶從范圍中選擇一個值。通過系統提供的Slider&#xff0c;用起來也很方便。 Slider 先看一個最簡單的初始化方法&#xff1a; State private var sliderValue: Float 100var body: some View {V…

[AIGC] mac os 中 .DS_Store 是什么

.DS_Store 是在 MacOS 系統中由 Finder 應用程序創建和維護的一種隱藏文件&#xff0c;用于保存有關其所在目錄的自定義屬性&#xff0c;例如圖標位置或背景顏色。 “.DS_Store” 是 “Desktop Services Store” 的縮寫。 .DS_Store 的作用 .DS_Store 文件在每個 Mac OS X 文…

ollama 使用,以及指定模型下載地址

ollama windows 使用 官網&#xff1a; https://ollama.com/ windows 指定 models 下載地址 默認會下載在C盤 &#xff0c;占用空間 在Windows系統中&#xff0c;可以通過設置環境變量OLLAMA_MODELS來指定模型文件的下載和存儲路徑。具體操作步驟如下&#xff1a; 1.打開系統…

【python006】miniconda3環境搭建(非root目錄,最近更新中)

1.熟悉、梳理、總結項目研發實戰中的Python開發日常使用中的問題。 2.歡迎點贊、關注、批評、指正&#xff0c;互三走起來&#xff0c;小手動起來&#xff01; 文章目錄 1.背景介紹2. 1.背景介紹 環境移植&#xff0c;可能影響部署本機環境信息&#xff0c;探索、總結移植有效…

輕量化微調相關學習

輕量化微調&#xff08;Lightweight Fine-Tuning&#xff09;是指在大型預訓練模型基礎上&#xff0c;通過修改或添加少量參數來進行模型適應性調整的一種方法&#xff0c;旨在減少計算資源消耗和避免過擬合問題&#xff0c;同時保持模型的性能。這種方法特別適用于資源有限或需…