Java中Stream流的常用方法代碼示例
stream流簡(jiǎn)介
stream流操作是Java 8提供一個(gè)重要新特性,它允許開(kāi)發(fā)人員以聲明性方式處理集合,其核心類(lèi)庫(kù)主要改進(jìn)了對(duì)集合類(lèi)的 API和新增Stream操作。
Stream類(lèi)中每一個(gè)方法都對(duì)應(yīng)集合上的一種操作。將真正的函數(shù)式編程引入到Java中,能 讓代碼更加簡(jiǎn)潔,極大地簡(jiǎn)化了集合的處理操作,提高了開(kāi)發(fā)的效率和生產(chǎn)力。
基本數(shù)據(jù)
自定義實(shí)體
@Data class Student{ private String name; private Integer age; private Double height; public Student() { } }
假數(shù)據(jù)
Student s1 = new Student(); s1.setAge(20); s1.setName("cookie"); s1.setHeight(180d); Student s2 = new Student(); s2.setAge(30); s2.setName("cookie"); s2.setHeight(180d); Student s3 = new Student(); s3.setAge(40); s3.setName("bob"); s3.setHeight(175d); Student s4 = new Student(); s4.setAge(40); s4.setName("bob"); s4.setHeight(180d); // 存入list集合 List<Student> list = new ArrayList<>(); list.add(s1); list.add(s2); list.add(s3); list.add(s4);
一、分組
1. 一層分組/簡(jiǎn)單分組
/** * 需求一(一層分組):根據(jù)Age分組 */ System.out.println("需求一(一層分組):根據(jù)Age分組"); Map<Integer, List<Student>> collect = list.stream().collect(Collectors.groupingBy(Student::getAge)); for (Integer age : collect.keySet()) { System.out.println("key:" + age + "\tvalue:" + collect.get(age)); } /** * 控制臺(tái)結(jié)果: * key:20 value:[Student(name=cookie, age=20, height=180.0)] * key:40 value:[Student(name=bob, age=40, height=175.0), Student(name=bob, age=40, height=180.0)] * key:30 value:[Student(name=cookie, age=30, height=180.0)] */
2. 多層分組
/** * 需求二: 先根據(jù)name分組,然后再根據(jù)身高分組 */ System.out.println("需求二: 先根據(jù)name分組,然后再根據(jù)身高分組"); Map<String, Map<Double, List<Student>>> collect1 = list.stream() .collect(Collectors.groupingBy(Student::getName, Collectors.groupingBy(Student::getHeight))); Set<String> namesGroup = collect1.keySet(); for (String namekey : namesGroup) { Map<Double, List<Student>> heightGroupMap = collect1.get(namekey); Set<Double> height = heightGroupMap.keySet(); for (Double h : height) { System.out.println("name:" + namekey + " height:" + heightGroupMap.get(h)); } } /** * 控制臺(tái)結(jié)果: * name:bob height:[Student(name=bob, age=40, height=175.0)] * name:bob height:[Student(name=bob, age=40, height=180.0)] * name:cookie height:[Student(name=cookie, age=20, height=180.0), Student(name=cookie, age=30, height=180.0)] */
3. 多層分組-自定義key
/** * 需求三: 自定義key返回 形式如下: age_height bob_175 */ System.out.println("需求三: 自定義key返回 形式如下: age_height bob_175"); Map<String, List<Student>> collect2 = list.stream() .collect(Collectors.groupingBy(c -> c.getName() + "_" + c.getHeight())); for (String customKey : collect2.keySet()) { System.out.println("key:" + customKey +" value:"+ collect2.get(customKey)); } /** * 控制臺(tái)結(jié)果: * key:bob_180.0 value:[Student(name=bob, age=40, height=180.0)] * key:bob_175.0 value:[Student(name=bob, age=40, height=175.0)] * key:cookie_180.0 value:[Student(name=cookie, age=20, height=180.0), Student(name=cookie, age=30, height=180.0)] */
二、排序
方式一: 通過(guò)自定義的比較器(非必要不推薦)
/** * 需求: 根據(jù)身高排序,如果身高相同,根據(jù)年齡排序,如果年齡依然相同,根據(jù)名稱字母順序排序 */ List<Student> collect3 = list.stream().sorted(new Comparator<Student>() { @Override public int compare(Student o1, Student o2) { // 這里前面的減去后面的是升序, 反之這是降序 if (!o1.getHeight().equals(o2.getHeight())) { return (int) (o1.getHeight() - o2.getHeight()); } if (!o1.getAge().equals(o2.getAge())) { return o1.getAge() - o2.getAge(); } return o1.getName().compareTo(o2.getName()); } }).collect(Collectors.toList()); System.out.println(collect3); /** * 控制臺(tái)結(jié)果: * [Student(name=bob, age=40, height=175.0), * Student(name=cookie, age=20, height=180.0), * Student(name=cookie, age=30, height=180.0), * Student(name=bob, age=40, height=180.0)] */ // 注: 當(dāng)然上面的也可以做一個(gè)簡(jiǎn)化 List<Student> collect3 = list.stream().sorted((o1, o2) -> { // 這里前面的減去后面的是升序, 反之這是降序 if (!o1.getHeight().equals(o2.getHeight())) { return (int) (o1.getHeight() - o2.getHeight()); } if (!o1.getAge().equals(o2.getAge())) { return o1.getAge() - o2.getAge(); } return o1.getName().compareTo(o2.getName()); }).collect(Collectors.toList());
方式二: 通過(guò)lambda
List<Student> collect4 = list.stream() .sorted(Comparator.comparingDouble(Student::getHeight) .thenComparingInt(Student::getAge) .thenComparing(Student::getName)) .collect(Collectors.toList()); System.out.println(collect4); /** * 控制臺(tái)結(jié)果: * [Student(name=bob, age=40, height=175.0), * Student(name=cookie, age=20, height=180.0), * Student(name=cookie, age=30, height=180.0), * Student(name=bob, age=40, height=180.0)] */ // 注意: // 方式一,升序降序是通過(guò)返回的正負(fù), // 方式二而是通過(guò)方法, 現(xiàn)在我們首先通過(guò)身高降序, 我們只需要在條件的后面加一個(gè)reversed()后綴方法即可 List<Student> collect4 = list.stream().sorted(Comparator.comparingDouble(Student::getHeight).reversed() .thenComparingInt(Student::getAge) .thenComparing(Student::getName) ).collect(Collectors.toList()); System.out.println(collect4); /** * 修改之后控制臺(tái)結(jié)果: * [Student(name=cookie, age=20, height=180.0), * Student(name=cookie, age=30, height=180.0), * Student(name=bob, age=40, height=180.0), * Student(name=bob, age=40, height=175.0)] */
三、 統(tǒng)計(jì)
/** * 需求: 統(tǒng)計(jì)年齡之和 */ int ageSum = list.stream().mapToInt(Student::getAge).sum(); /** * 求年齡平均值 */ Double ageAvg1 = list.stream().collect(Collectors.averagingInt(Student::getAge)); // 或者 double ageAvg2 = list.stream().mapToInt(Student::getAge).average().getAsDouble(); /** * 求年齡最大值 */ int maxAge = list.stream().mapToInt(Student::getAge).max().getAsInt(); /** * 最小值 */ int minAge = list.stream().mapToInt(Student::getAge).min().getAsInt();
到此這篇關(guān)于Java中Stream流的常用方法代碼示例的文章就介紹到這了,更多相關(guān)Stream流的常用方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java集合ArrayDeque類(lèi)實(shí)例分析
這篇文章主要介紹了Java集合ArrayDeque類(lèi)實(shí)例分析的相關(guān)資料,需要的朋友可以參考下2017-04-04mybatis中的擴(kuò)展實(shí)現(xiàn)源碼解析
這篇文章主要介給大家紹了關(guān)于mybatis中擴(kuò)展實(shí)現(xiàn)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01JVM內(nèi)存結(jié)構(gòu)劃分實(shí)例解析
這篇文章主要介紹了JVM內(nèi)存結(jié)構(gòu)劃分實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12Java強(qiáng)制保留兩位小數(shù)的四種方法案例詳解
這篇文章主要介紹了Java強(qiáng)制保留兩位小數(shù)的四種方法案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09IDEA設(shè)置字體隨鼠標(biāo)滾動(dòng)放大縮小的實(shí)現(xiàn)
這篇文章主要介紹了IDEA設(shè)置字體隨鼠標(biāo)滾動(dòng)放大縮小的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01SpringBoot+Vue前后端分離實(shí)現(xiàn)請(qǐng)求api跨域問(wèn)題
這篇文章主要介紹了SpringBoot+Vue前后端分離實(shí)現(xiàn)請(qǐng)求api跨域問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06Java實(shí)現(xiàn)簡(jiǎn)易版猜燈謎游戲的示例代碼
燈謎是中秋節(jié)傳統(tǒng)的活動(dòng)之一,而現(xiàn)代化的方式則是將其制作成一個(gè)小游戲,讓用戶在游戲的過(guò)程中猜燈謎,互動(dòng)體驗(yàn)更佳,所以本文小編就用Java制作一款猜燈謎小游戲吧2023-09-09