第十三節:第七部分:Stream流的中間方法、Stream流的終結方法

Stream流常見的中間方法

Stream流常見的中間方法

Stream流常見的終結方法

Stream流常見的終結方法
Stream流常見的終結方法2

代碼

學生類(代碼一與代碼二共涉及到的類)

package com.itheima.day28_Stream;import java.util.Objects;public class Student implements Comparable<Student> {private String name;private int age;private double height;@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +", height=" + height +'}';}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Student student = (Student) o;return age == student.age && Double.compare(height, student.height) == 0 && Objects.equals(name, student.name);}@Overridepublic int hashCode() {return Objects.hash(name, age, height);}public Student() {}public Student(String name, int age, double height) {this.name = name;this.age = age;this.height = height;}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 double getHeight() {return height;}public void setHeight(double height) {this.height = height;}@Overridepublic int compareTo(Student o) {return this.age - o.age;//年齡升序排序}
}

代碼一:掌握Stream流提供的常見中間方法

package com.itheima.day28_Stream;import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;//目標:掌握Stream流提供的常見中間方法
public class StreamTest3 {public static void main(String[] args) {List<Double> scores = new ArrayList<>();Collections.addAll(scores,88.5,56.6,58.8,89.9,100.0,16.6);// 需求1:找出成績大于等于60分的數據,并升序后,再輸出。scores.stream().filter(s->s>=60).sorted().forEach(s->System.out.println(s));List<Student>students =new ArrayList<>();Student s1 = new Student("蜘蛛精",26,172.5);Student s2 = new Student("蜘蛛精",26, 172.5);Student s3 = new Student( "紫霞",23, 167.6);Student s4 = new Student("白晶品", 25,169.0);Student s5 = new Student("牛魔王",35,183.3);Student s6 =new Student( "牛夫人", 34,168.5);Collections.addAll(students,s1,s2,s3,s4,s5,s6);// 需求2:找出年齡大于等于23,且年齡小于等于30歲的學生,并按照年齡降序輸出.students.stream().filter(s -> s.getAge()>=23 && s.getAge()<=30).sorted((o1,o2)-> o2.getAge()-o1.getAge())//使用lambda簡寫,年齡降序排列.forEach(s->System.out.println(s));System.out.println("===========================================");//需求3:取出身高最高的前3名學生,并輸出。students.stream().sorted((o1,o2)->Double.compare(o2.getHeight(),o1.getHeight())).limit(3).forEach(System.out::println);System.out.println("===========================================");// 需求4:取出身高倒數的2名學生,并輸出。students.stream().sorted((o1,o2)->Double.compare(o2.getHeight(),o1.getHeight())).skip(students.size()-2).forEach(System.out::println);System.out.println("===========================================");// 需求5:找出身高超過168的學生叫什么名字,要求去除重復的名字,再輸出。students.stream().filter(s->s.getHeight()>168).map(Student::getName)//把原來的學生對象集合加工(映射)成為名字集合.distinct()//去除重復的名字.forEach(System.out::println);System.out.println("===========================================");// distinct去重復,自定義類型的對象(希望內容一樣就認為重復,重寫hashCode,equals)students.stream().distinct().forEach(System.out::println);System.out.println("===========================================");//合并兩個stream流Stream<String> str1 = Stream.of("張三", "李四");Stream<String> str2 = Stream.of("張3", "李4","王五");Stream<String> Allst = Stream.concat(str1, str2);Allst.forEach(System.out::println);}
}

結果1.1
結果1.2

代碼二:掌握Stream流提供的常見終結方法

package com.itheima.day28_Stream;import java.util.*;
import java.util.stream.Collectors;
//目標:掌握Stream流提供的常見終結方法
public class StreamTest4 {public static void main(String[] args) {List<Student> students =new ArrayList<>();Student s1 = new Student("蜘蛛精",26,172.5);Student s2 = new Student("蜘蛛精",26, 172.5);Student s3 = new Student( "紫霞",23, 167.6);Student s4 = new Student("白晶品", 25,169.0);Student s5 = new Student("牛魔王",35,183.3);Student s6 =new Student( "牛夫人", 34,168.5);Collections.addAll(students,s1,s2,s3,s4,s5,s6);// 需求1:請計算出身高超過168的學生有幾人。long count = students.stream().filter(s -> s.getHeight() > 168).count();System.out.println(count);System.out.println("===================================");// 需求2:請找出身高最高的學生對象,并輸出。Student stu1 = students.stream().max((o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight())).get();System.out.println(stu1);System.out.println("===================================");// 需求3:請找出身高最矮的學生對象,并輸出。Student stu2 = students.stream().min((o1, o2) -> Double.compare(o1.getHeight(), o2.getHeight())).get();System.out.println(stu2);System.out.println("===================================");// 需求4:請找出身高超過170的學生對象,并放到一個新集合中去返回。//流只能收集一次,因此"students.stream().filter(s -> s.getHeight() > 170)"不能單獨作為新的stream流分別取做collect(Collectors.toList())與collect(Collectors.toSet())操作List<Student> students1 = students.stream().filter(s -> s.getHeight() > 170).collect(Collectors.toList());System.out.println(students1);Set<Student> students2 = students.stream().filter(s -> s.getHeight() > 170).collect(Collectors.toSet());System.out.println(students2);// 需求5:請找出身高超過170的學生對象,并把學生對象的名字和身高,存入到一個Map集合返回。System.out.println("===================================");Map<String, Double> map = students.stream().filter(s -> s.getHeight() > 170).distinct()//map集合的元素不可重復,因此先去重.collect(Collectors.toMap(a -> a.getName(), a -> a.getHeight()));System.out.println(map);System.out.println("===================================");//存入到數組//寫法1Object[] array1 = students.stream().filter(s -> s.getHeight() > 170).toArray();System.out.println(Arrays.toString(array1));System.out.println("======================================");//寫法2Student[] array2 = students.stream().filter(s -> s.getHeight() > 170).toArray(len -> new Student[len]);System.out.println(Arrays.toString(array2));}
}

結果2

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

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

相關文章

深入理解 Go 中的字節序(Endianness)檢測代碼

深入理解 Go 中的字節序&#xff08;大小端&#xff09;檢測代碼 在計算機系統中&#xff0c;字節序&#xff08;Endianness&#xff09; 是指多字節數據類型&#xff08;如 int16、int32 等&#xff09;在內存中的存儲順序。Go 語言標準庫提供了對大端&#xff08;Big-endian&…

JAVA:RabbitMQ 消息持久化機制的技術指南

?? 1、簡述 在使用 RabbitMQ 構建可靠消息系統時,消息丟失是必須避免的問題。為此,RabbitMQ 提供了消息持久化機制(Message Durability),可以保障在 Broker 異常宕機后數據不會丟失。 本篇博客將從原理出發,結合 Spring Boot 實戰講解如何正確實現 RabbitMQ 消息持久…

tabs頁簽嵌套表格,切換表格保存數據不變并回勾

需求&#xff1a;點擊左邊的tab頁簽&#xff0c;請求右側表格數據&#xff1b;如果返回的接口數據存在taskuser字段并不為null&#xff0c;那么按照這個字段去回勾數據。如果存在數據&#xff0c;但與后面所勾選的數據項不同&#xff0c;按照后面勾選的為主。 <el-tabs tab-…

Java Kafka消費者

基礎 Java Kafka消費者主要通過以下核心類實現&#xff1a; KafkaConsumer&#xff1a;消費者的核心類&#xff0c;用于創建消費者對象進行數據消費1ConsumerConfig&#xff1a;獲取各種配置參數&#xff0c;如果不配置就使用默認值1ConsumerRecord&#xff1a;每條數據都要封…

Git操作問題及解決方案-記錄5

Git操作問題及解決方案 問題一&#xff1a;本地更改與遠程更新沖突 問題描述 當本地文件有未提交的更改&#xff0c;同時遠程倉庫也有更新時&#xff0c;執行git pull會導致沖突。 $ git pull origin main error: Your local changes to the following files would be overw…

一[3]、ubuntu18.04環境 利用 yolov8 訓練開源列車數據集,并實現列車軌道檢測

一、開源車載數據集地址 (7 封私信) 軌道交通數據集-OSDaR23: Open Sensor Data for Rail 2023 - 知乎 二、參考資料 https://zhuanlan.zhihu.com/p/692608487 YOLOv8訓練自己的數據集-CSDN博客 https://download.csdn.net/blog/column/12710137/140991739

C語言數據結構筆記5:Keil 編譯器優化行為_malloc指針內存分配問題

記錄倆個keil5 STM32 的c語言編程中 &#xff0c;編譯器優化行為 和 指針內存分配問題。 目錄 關閉Keil 編譯器優化行為&#xff1a; malloc指針內存分配問題 多層嵌套的結構體&#xff1a; 用指針取值&#xff1a; 發現問題&#xff1a; 解決問題&#xff1a; 示例代碼 關閉Ke…

每日八股文6.12

每日八股-6.12 計算機網絡1.當我們在瀏覽器中輸入一個 URL 并按下回車后&#xff0c;到頁面最終顯示出來&#xff0c;這中間都發生了哪些關鍵步驟&#xff1f;2.請簡述一下 JWT&#xff08;JSON Web Tokens&#xff09;的原理和校驗機制3.DNS 是如何進行域名解析的&#xff1f;…

什么是云計算的邊緣原生應用?

關于作者&#xff1a;John Bradshaw阿卡邁公司歐洲、中東和非洲地區云計算技術與戰略總監 當談及云計算時&#xff0c;人們往往會聯想到那些坐落于國際大都會核心地帶的大型數據中心集群&#xff0c;這些設施作為數字時代的重要樞紐&#xff0c;承載著海量數據處理任務。盡管這…

Linux常用命令速查與面試高頻命令總結

&#x1f427; Linux常用命令速查與面試高頻命令總結 本文旨在幫助初學者快速掌握 Linux 的常用命令&#xff0c;同時為即將參加技術面試的朋友們提供一份高頻命令清單和實用技巧。 &#x1f530; 一、基礎命令&#xff1a;熟練使用命令行從這里開始 這些是你在 Linux 中最常用…

基礎測試工具使用經驗

背景 vtune&#xff0c;perf, nsight system等基礎測試工具&#xff0c;都是用過的&#xff0c;但是沒有記錄&#xff0c;都逐漸忘了。所以寫這篇博客總結記錄一下&#xff0c;只要以后發現新的用法&#xff0c;就記得來編輯補充一下 perf 比較基礎的用法&#xff1a; 先改這…

淺談DaemonSet

1. DaemonSet 概述 ?定義?&#xff1a;DaemonSet 確保 Kubernetes 集群的每個節點上運行一個 Pod 實例。?特性?&#xff1a; 每個節點上只有一個 Pod 實例。新節點加入集群時&#xff0c;會自動在新節點上創建 Pod。舊節點被刪除時&#xff0c;其上的 Pod 會被回收。 2.…

計算機系統(6)

◆指令尋址方式&#xff1a; 順序尋址方式&#xff1a;執行一段程序時&#xff0c;是一條指令接著一條指令的順序執行。 跳躍尋址方式:下一條指令的地址碼不是由程序計數器給出&#xff0c;而是由本條指令直接給出。程序跳躍后&#xff0c;按新的指令地址開始順序執行。因此&…

基于服務器使用 apt 安裝、配置 Nginx

&#x1f9fe; 一、查看可安裝的 Nginx 版本 首先&#xff0c;你可以運行以下命令查看可用版本&#xff1a; apt-cache madison nginx-core輸出示例&#xff1a; nginx-core | 1.18.0-6ubuntu14.6 | http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages ng…

python打卡訓練營打卡記錄day51

復習日 作業&#xff1a;day43的時候我們安排大家對自己找的數據集用簡單cnn訓練&#xff0c;現在可以嘗試下借助這幾天的知識來實現精度的進一步提高 數據預處理 import torch import torch.nn as nn import torch.optim as optim from torchvision import datasets, transfor…

網絡安全:OWASP防護守則

目錄 一、OWASP十大WEB弱點防護守則 二、防護守則 1、權限控制失效 2、加密失誤 3、注入 4、不安全設計 5、安全配置缺陷 6、易受攻擊和過時的組件 7、身份認證和會話管理失效 8、缺乏完整性校驗 9、缺乏監控與日志記錄 10、服務端請求偽造 三、核心防護原則總結 …

Dagster 實現數據質量自動化:6大維度檢查與最佳實踐

在當今數據驅動的世界中&#xff0c;數據質量的重要性不言而喻。數據質量測試是確保數據準確、完整、一致和可靠的關鍵步驟。本文將深入探討數據質量測試的六大維度&#xff0c;并提供相關的檢查方法和最佳實踐。 什么是數據質量測試&#xff1f; 數據質量測試涉及評估數據以確…

計算機視覺之三維重建(深入淺出SfM與SLAM核心算法)—— 2. 攝像機標定

文章目錄 1. 前置知識1.1. 非齊次線性方程組求解1.1.1. 傳統求解方法1.1.2. 奇異值分解法1.1.3. 牛頓法或者梯度下降法 1.2. 齊次線性方程組的最小二乘解1.3. 非線性方程組的最小二乘解 2. 相機標定2.1. 相機內參數求解2.1.1. 求解 u 0 u_0 u0? 和 v 0 v_0 v0?2.1.2. 求解 …

SQLLL

595-big-countries https://leetcode.com/problems/big-countries/description/ 面積最大的國家 -- select name, population, area from World where area > 3000000 or population > 25000000596-classes-with-at-least-5-students https://leetcode.com/problems/…

MySQL中觸發器詳解 觸發器在自動化任務中的應用場景

觸發器是mysql中與表關聯的數據庫對象&#xff0c;能在特定操作&#xff08;如insert、update、delete&#xff09;發生時自動執行預定義sql邏輯。其核心用途包括&#xff1a;1. 維護數據一致性&#xff0c;如訂單插入后自動減少庫存&#xff1b;2. 記錄審計日志&#xff0c;如…