posts - 325,  comments - 25,  trackbacks - 0
          *
           * 一、Stream API 的操作步驟:
           *
           * 1. 創(chuàng)建 Stream
           *
           * 2. 中間操作
           *
           * 3. 終止操作(終端操作)
           */
          public class TestStreamaAPI {
           
           //1. 創(chuàng)建 Stream
           @Test
           public void test1(){
            //1. Collection 提供了兩個(gè)方法  stream() 與 parallelStream()
            List<String> list = new ArrayList<>();
            Stream<String> stream = list.stream(); //獲取一個(gè)順序流
            Stream<String> parallelStream = list.parallelStream(); //獲取一個(gè)并行流
            
            //2. 通過(guò) Arrays 中的 stream() 獲取一個(gè)數(shù)組流
            Integer[] nums = new Integer[10];
            Stream<Integer> stream1 = Arrays.stream(nums);
            
            //3. 通過(guò) Stream 類中靜態(tài)方法 of()
            Stream<Integer> stream2 = Stream.of(1,2,3,4,5,6);
            
            //4. 創(chuàng)建無(wú)限流
            //迭代
            Stream<Integer> stream3 = Stream.iterate(0, (x) -> x + 2).limit(10);
            stream3.forEach(System.out::println);
            
            //生成
            Stream<Double> stream4 = Stream.generate(Math::random).limit(2);
            stream4.forEach(System.out::println);
            
           }
           
           //2. 中間操作
           List<Employee> emps = Arrays.asList(
             new Employee(102, "李四", 59, 6666.66),
             new Employee(101, "張三", 18, 9999.99),
             new Employee(103, "王五", 28, 3333.33),
             new Employee(104, "趙六", 8, 7777.77),
             new Employee(104, "趙六", 8, 7777.77),
             new Employee(104, "趙六", 8, 7777.77),
             new Employee(105, "田七", 38, 5555.55)
           );
           
           /*
             篩選與切片
            filter——接收 Lambda , 從流中排除某些元素。
            limit——截?cái)嗔鳎蛊湓夭怀^(guò)給定數(shù)量。
            skip(n) —— 跳過(guò)元素,返回一個(gè)扔掉了前 n 個(gè)元素的流。若流中元素不足 n 個(gè),則返回一個(gè)空流。與 limit(n) 互補(bǔ)
            distinct——篩選,通過(guò)流所生成元素的 hashCode() 和 equals() 去除重復(fù)元素
            */
           
           //內(nèi)部迭代:迭代操作 Stream API 內(nèi)部完成
           @Test
           public void test2(){
            //所有的中間操作不會(huì)做任何的處理
            Stream<Employee> stream = emps.stream()
             .filter((e) -> {
              System.out.println("測(cè)試中間操作");
              return e.getAge() <= 35;
             });
            
            //只有當(dāng)做終止操作時(shí),所有的中間操作會(huì)一次性的全部執(zhí)行,稱為“惰性求值”
            stream.forEach(System.out::println);
           }
           
           //外部迭代
           @Test
           public void test3(){
            Iterator<Employee> it = emps.iterator();
            
            while(it.hasNext()){
             System.out.println(it.next());
            }
           }
           
           @Test
           public void test4(){
            emps.stream()
             .filter((e) -> {
              System.out.println("短路!"); // &&  ||
              return e.getSalary() >= 5000;
             }).limit(3)
             .forEach(System.out::println);
           }
           
           @Test
           public void test5(){
            emps.parallelStream()
             .filter((e) -> e.getSalary() >= 5000)
             .skip(2)
             .forEach(System.out::println);
           }
           
           @Test
           public void test6(){
            emps.stream()
             .distinct()
             .forEach(System.out::println);
           }
          posted on 2018-03-06 08:36 長(zhǎng)春語(yǔ)林科技 閱讀(130) 評(píng)論(0)  編輯  收藏 所屬分類: java8
          <2018年3月>
          25262728123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

           

          長(zhǎng)春語(yǔ)林科技?xì)g迎您!

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊(cè)

          收藏夾

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 乐东| 封开县| 临城县| 凉城县| 绥阳县| 丹阳市| 南充市| 泰来县| 肥东县| 榆社县| 大兴区| 康平县| 阿拉尔市| 南京市| 浑源县| 称多县| 慈溪市| 六盘水市| 通山县| 轮台县| 江山市| 会理县| 冀州市| 桃江县| 双江| 阿鲁科尔沁旗| 股票| 长海县| 宜兴市| 洪湖市| 峨眉山市| 满城县| 河津市| 娱乐| 泾源县| 剑川县| 钟祥市| 于都县| 普陀区| 乌鲁木齐县| 泾源县|