<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          求求你別在用 for循環(huán)遍歷list 了,真的太low了!

          共 12249字,需瀏覽 25分鐘

           ·

          2022-09-17 22:01

          點(diǎn)擊關(guān)注公眾號(hào):互聯(lián)網(wǎng)架構(gòu)師,后臺(tái)回復(fù) 2T獲取2TB學(xué)習(xí)資源!
          上一篇:Alibaba開源內(nèi)網(wǎng)高并發(fā)編程手冊(cè).pdf

          簡介

          Java 8 API添加了一個(gè)新的抽象稱為流Stream,可以讓你以一種聲明的方式處理數(shù)據(jù)。
          Stream 使用一種類似用 SQL 語句從數(shù)據(jù)庫查詢數(shù)據(jù)的直觀方式來提供一種對(duì) Java 集合運(yùn)算和表達(dá)的高階抽象。這種風(fēng)格將要處理的元素集合看作一種流, 流在管道中傳輸, 并且可以在管道的節(jié)點(diǎn)上進(jìn)行處理, 比如篩選, 排序,聚合等。

          熟悉Linux的同學(xué)對(duì)這種風(fēng)格一定不陌生,因?yàn)樗鶯inux的|管道符的思想如出一轍。上面這段話引用自runoob.com,但是其教學(xué)代碼都是基于String列表進(jìn)行演示,考慮到實(shí)際情況百分之80的時(shí)候都是對(duì)PO、VO進(jìn)行處理,因此以下通過一個(gè)PO進(jìn)行講解。

          對(duì)比起for循環(huán)操作list,最大的弊端就是代碼太長太亂了,如果涉及3-4張表的操作,也就是涉及多個(gè)PO操作,那個(gè)括號(hào)簡直就是俄羅斯套娃,寫到最后真的自己都不知道在寫什么

          +--------------------+       +------+   +------+   +---+   +-------+
          | stream of elements +-----> |filter+-> |sorted+-> |map+-> |collect|
          +--------------------+       +------+   +------+   +---+   +-------+

          PO代碼

          public class UserPo {
              private String name;
              private Double score;
              
              // 省略構(gòu)造函數(shù)及getter、setter
          }

          以下操作均以UserPo進(jìn)行講解

          filter

          filter:過濾,就是過濾器,符合條件的通過,不符合條件的過濾掉

          // 篩選出成績不為空的學(xué)生人數(shù)
          count = list.stream().filter(p -> null != p.getScore()).count();

          map

          map:映射,他將原集合映射成為新的集合,在VO、PO處理的過程中較常見。在本例子中,原集合就是PO集合,新集合可以自定義映射為成績集合,同時(shí)也可以對(duì)新集合進(jìn)行相關(guān)操作

          // 取出所有學(xué)生的成績
          List<Double> scoreList = list.stream().map(p -> p.getScore()).collect(Collectors.toList());

          // 將學(xué)生姓名集合串成字符串,用逗號(hào)分隔
          String nameString = list.stream().map(p -> p.getName()).collect(Collectors.joining(","));

          sorted

          sorted:排序,可以根據(jù)指定的字段進(jìn)行排序

          // 按學(xué)生成績逆序排序 正序則不需要加.reversed()
          filterList = list.stream().filter(p -> null != p.getScore()).sorted(Comparator.comparing(UserPo::getScore).reversed()).collect(Collectors.toList());

          forEach

          forEach:這個(gè)應(yīng)該是最常用的,也就是為每一個(gè)元素進(jìn)行自定義操作

          除了forEach操作會(huì)改變?cè)系臄?shù)據(jù),其他的操作均不會(huì)改變?cè)?,這點(diǎn)務(wù)必引起注意

          // 學(xué)生成績太差了,及格率太低,給每個(gè)學(xué)生加10分,放個(gè)水
          // forEach
          filterList.stream().forEach(p -> p.setScore(p.getScore() + 10));

          collect

          collect:聚合,可以用于GroudBy按指定字段分類,也可以用于返回列表或者拼湊字符串

          // 按成績進(jìn)行歸集
          Map<Double, List<UserPo>> groupByScoreMap = list.stream().filter(p -> null != p.getScore()).collect(Collectors.groupingBy(UserPo::getScore));
          for (Map.Entry<Double, List<UserPo>> entry : groupByScoreMap.entrySet()) {
              System.out.println("成績:" + entry.getKey() + " 人數(shù):" + entry.getValue().size());
          }

          // 返回list
          List<Double> scoreList = list.stream().map(p -> p.getScore()).collect(Collectors.toList());
          // 返回string用逗號(hào)分隔
          String nameString = list.stream().map(p -> p.getName()).collect(Collectors.joining(","));

          statistics

          statistics:統(tǒng)計(jì),可以統(tǒng)計(jì)中位數(shù),平均值,最大最小值

          DoubleSummaryStatistics statistics = filterList.stream().mapToDouble(p -> p.getScore()).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());

          parallelStream

          parallelStream:并行流,可以利用多線程進(jìn)行流的操作,提升效率。但是其不具備線程傳播性,因此使用時(shí)需要充分評(píng)估是否需要用并行流操作

          // 并行流
          count = list.parallelStream().filter(p -> null != p.getScore()).count();

          完整代碼

          package com.cmx.tcn.stream;

          /**
           * @author: Cai MinXing
           **/

          public class UserPo {
              private String name;
              private Double score;

              public UserPo(String name, Double score) {
                  this.name = name;
                  this.score = score;
              }

              public String getName() {
                  return name;
              }

              public void setName(String name) {
                  this.name = name;
              }

              public Double getScore() {
                  return score;
              }

              public void setScore(Double score) {
                  this.score = score;
              }

              @Override
              public String toString() {
                  return "UserPo{" +
                          "name='" + name + '\'' +
                          ", score=" + score +
                          '}';
              }
          }
          package com.cmx.tcn.stream;

          import java.util.ArrayList;
          import java.util.Comparator;
          import java.util.DoubleSummaryStatistics;
          import java.util.List;
          import java.util.Map;
          import java.util.stream.Collectors;

          /**
           * @author: Cai MinXing
           * @create: 2020-03-25 18:15
           **/

          public class StreamTest {

          //    +--------------------+       +------+   +------+   +---+   +-------+
          //    | stream of elements +-----> |filter+-> |sorted+-> |map+-> |collect|
          //    +--------------------+       +------+   +------+   +---+   +-------+

              public static void main(String args[]){

                  List<UserPo> list = new ArrayList<>();
                  list.add(new UserPo("小一"10.d));
                  list.add(new UserPo("小五"50.d));
                  list.add(new UserPo("小六"60.d));
                  list.add(new UserPo("小6"60.d));
                  list.add(new UserPo("小空"null));
                  list.add(new UserPo("小九"90.d));

                  long count = 0;
                  List<UserPo> filterList = null;

                  // filter 過濾器的使用
                  // 篩選出成績不為空的學(xué)生人數(shù)
                  count = list.stream().filter(p -> null != p.getScore()).count();
                  System.out.println("參加考試的學(xué)生人數(shù):" + count);

                  // collect
                  // 篩選出成績不為空的學(xué)生集合
                  filterList = list.stream().filter(p -> null != p.getScore()).collect(Collectors.toList());
                  System.out.println("參加考試的學(xué)生信息:");
                  filterList.stream().forEach(System.out::println);

                  // map 將集合映射為另外一個(gè)集合
                  // 取出所有學(xué)生的成績
                  List<Double> scoreList = list.stream().map(p -> p.getScore()).collect(Collectors.toList());
                  System.out.println("所有學(xué)生的成績集合:" + scoreList);

                  // 將學(xué)生姓名集合串成字符串,用逗號(hào)分隔
                  String nameString = list.stream().map(p -> p.getName()).collect(Collectors.joining(","));
                  System.out.println("所有學(xué)生的姓名字符串:" + nameString);

                  // sorted排序
                  // 按學(xué)生成績逆序排序 正序則不需要加.reversed()
                  filterList = list.stream().filter(p -> null != p.getScore()).sorted(Comparator.comparing(UserPo::getScore).reversed()).collect(Collectors.toList());
                  System.out.println("所有學(xué)生的成績集合,逆序排序:");
                  filterList.stream().forEach(System.out::println);

                  System.out.println("按學(xué)生成績歸集:");
                  Map<Double, List<UserPo>> groupByScoreMap = list.stream().filter(p -> null != p.getScore())
                          .collect(Collectors.groupingBy(UserPo::getScore));
                  for (Map.Entry<Double, List<UserPo>> entry : groupByScoreMap.entrySet()) {
                      System.out.println("成績:" + entry.getKey() + " 人數(shù):" + entry.getValue().size());
                  }

                  // forEach
                  filterList.stream().forEach(p -> p.setScore(p.getScore() + 10));
                  System.out.println("及格人數(shù)太少,給每個(gè)人加10分");
                  filterList.stream().forEach(System.out::println);

                  // count
                  count = filterList.stream().filter(p -> p.getScore() >= 60).count();
                  System.out.println("最后及格人數(shù)" + count);

                  DoubleSummaryStatistics statistics = filterList.stream().mapToDouble(p -> p.getScore()).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());

                  // 并行流 使用
                  count = list.parallelStream().filter(p -> null != p.getScore()).count();
                  System.out.println("并行流處理參加考試的學(xué)生人數(shù):" + count);

              }

          }
          原文:bugpool.blog.csdn.net/article/details/105122681

          -End-
          最后,關(guān)注公眾號(hào)互聯(lián)網(wǎng)架構(gòu)師,在后臺(tái)回復(fù):2T,可以獲取我整理的 Java 系列面試題和答案,非常齊全。


          正文結(jié)束


          推薦閱讀 ↓↓↓

          1.求求你別在用SpringMVC了,太Low了!Spring又官宣了一個(gè)更牛逼的替代框架!

          2.從零開始搭建創(chuàng)業(yè)公司后臺(tái)技術(shù)棧

          3.程序員一般可以從什么平臺(tái)接私活?

          4.Spring Boot+Redis+攔截器+自定義Annotation實(shí)現(xiàn)接口自動(dòng)冪等

          5.為什么國內(nèi) 996 干不過國外的 955呢?

          6.中國的鐵路訂票系統(tǒng)在世界上屬于什么水平?                        

          7.15張圖看懂瞎忙和高效的區(qū)別!

          瀏覽 16
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  AV 无码 白丝 软件 | 天天干天天操天天射 | 亚洲欧美专区 | 天天av天天日天天干 | 国产无套内精一级毛片三 |