<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嗎?

          共 10055字,需瀏覽 21分鐘

           ·

          2022-07-17 03:02

          1

          簡介


          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)簡直就是俄羅斯套娃,寫到最后真的自己都不知道在寫什么。



          2


          +--------------------+ +------+ +------+ +---+ +-------+
          | 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();



          3

          完整代碼


          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 +
                          '}';
              }
          }


          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);

              }

          }


          來源:blog.csdn.net/chaitoudaren/article/details/105122681

          瀏覽 32
          點(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乱码在线观看 | 天天日夜夜 | 日韩一级色 |