<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 325,  comments - 25,  trackbacks - 0
    public class TestStreamAPI2 {
     
     List<Employee> emps = Arrays.asList(
       new Employee(102, "李四", 59, 6666.66, Status.BUSY),
       new Employee(101, "張三", 18, 9999.99, Status.FREE),
       new Employee(103, "王五", 28, 3333.33, Status.VOCATION),
       new Employee(104, "趙六", 8, 7777.77, Status.BUSY),
       new Employee(104, "趙六", 8, 7777.77, Status.FREE),
       new Employee(104, "趙六", 8, 7777.77, Status.FREE),
       new Employee(105, "田七", 38, 5555.55, Status.BUSY)
     );
     
     //3. 終止操作
     /*
      allMatch——檢查是否匹配所有元素
      anyMatch——檢查是否至少匹配一個元素
      noneMatch——檢查是否沒有匹配的元素
      findFirst——返回第一個元素
      findAny——返回當前流中的任意元素
      count——返回流中元素的總個數(shù)
      max——返回流中最大值
      min——返回流中最小值
      */
     @Test
     public void test1(){
       boolean bl = emps.stream()
        .allMatch((e) -> e.getStatus().equals(Status.BUSY));
       
       System.out.println(bl);
       
       boolean bl1 = emps.stream()
        .anyMatch((e) -> e.getStatus().equals(Status.BUSY));
       
       System.out.println(bl1);
       
       boolean bl2 = emps.stream()
        .noneMatch((e) -> e.getStatus().equals(Status.BUSY));
       
       System.out.println(bl2);
     }
     
     @Test
     public void test2(){
      Optional<Employee> op = emps.stream()
       .sorted((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()))
       .findFirst();
      
      System.out.println(op.get());
      
      System.out.println("--------------------------------");
      
      Optional<Employee> op2 = emps.parallelStream()
       .filter((e) -> e.getStatus().equals(Status.FREE))
       .findAny();
      
      System.out.println(op2.get());
     }
     
     @Test
     public void test3(){
      long count = emps.stream()
           .filter((e) -> e.getStatus().equals(Status.FREE))
           .count();
      
      System.out.println(count);
      
      Optional<Double> op = emps.stream()
       .map(Employee::getSalary)
       .max(Double::compare);
      
      System.out.println(op.get());
      
      Optional<Employee> op2 = emps.stream()
       .min((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
      
      System.out.println(op2.get());
     }
     
     //注意:流進行了終止操作后,不能再次使用
     @Test
     public void test4(){
      Stream<Employee> stream = emps.stream()
       .filter((e) -> e.getStatus().equals(Status.FREE));
      
      long count = stream.count();
      
      stream.map(Employee::getSalary)
       .max(Double::compare);
     }
    }


    public class TestStreamAPI3 {
     
     List<Employee> emps = Arrays.asList(
       new Employee(102, "李四", 79, 6666.66, Status.BUSY),
       new Employee(101, "張三", 18, 9999.99, Status.FREE),
       new Employee(103, "王五", 28, 3333.33, Status.VOCATION),
       new Employee(104, "趙六", 8, 7777.77, Status.BUSY),
       new Employee(104, "趙六", 8, 7777.77, Status.FREE),
       new Employee(104, "趙六", 8, 7777.77, Status.FREE),
       new Employee(105, "田七", 38, 5555.55, Status.BUSY)
     );
     
     //3. 終止操作
     /*
      歸約
      reduce(T identity, BinaryOperator) / reduce(BinaryOperator) ——可以將流中元素反復(fù)結(jié)合起來,得到一個值。
      */
     @Test
     public void test1(){
      List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
      
      Integer sum = list.stream()
       .reduce(0, (x, y) -> x + y);
      
      System.out.println(sum);
      
      System.out.println("----------------------------------------");
      
      Optional<Double> op = emps.stream()
       .map(Employee::getSalary)
       .reduce(Double::sum);
      
      System.out.println(op.get());
     }
     
     //需求:搜索名字中 “六” 出現(xiàn)的次數(shù)
     @Test
     public void test2(){
      Optional<Integer> sum = emps.stream()
       .map(Employee::getName)
       .flatMap(TestStreamAPI1::filterCharacter)
       .map((ch) -> {
        if(ch.equals('六'))
         return 1;
        else
         return 0;
       }).reduce(Integer::sum);
      
      System.out.println(sum.get());
     }
     
     //collect——將流轉(zhuǎn)換為其他形式。接收一個 Collector接口的實現(xiàn),用于給Stream中元素做匯總的方法
     @Test
     public void test3(){
      List<String> list = emps.stream()
       .map(Employee::getName)
       .collect(Collectors.toList());
      
      list.forEach(System.out::println);
      
      System.out.println("----------------------------------");
      
      Set<String> set = emps.stream()
       .map(Employee::getName)
       .collect(Collectors.toSet());
      
      set.forEach(System.out::println);

      System.out.println("----------------------------------");
      
      HashSet<String> hs = emps.stream()
       .map(Employee::getName)
       .collect(Collectors.toCollection(HashSet::new));
      
      hs.forEach(System.out::println);
     }
     
     @Test
     public void test4(){
      Optional<Double> max = emps.stream()
       .map(Employee::getSalary)
       .collect(Collectors.maxBy(Double::compare));
      
      System.out.println(max.get());
      
      Optional<Employee> op = emps.stream()
       .collect(Collectors.minBy((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary())));
      
      System.out.println(op.get());
      
      Double sum = emps.stream()
       .collect(Collectors.summingDouble(Employee::getSalary));
      
      System.out.println(sum);
      
      Double avg = emps.stream()
       .collect(Collectors.averagingDouble(Employee::getSalary));
      
      System.out.println(avg);
      
      Long count = emps.stream()
       .collect(Collectors.counting());
      
      System.out.println(count);
      
      System.out.println("--------------------------------------------");
      
      DoubleSummaryStatistics dss = emps.stream()
       .collect(Collectors.summarizingDouble(Employee::getSalary));
      
      System.out.println(dss.getMax());
     }
     
     //分組
     @Test
     public void test5(){
      Map<Status, List<Employee>> map = emps.stream()
       .collect(Collectors.groupingBy(Employee::getStatus));
      
      System.out.println(map);
     }
     
     //多級分組
     @Test
     public void test6(){
      Map<Status, Map<String, List<Employee>>> map = emps.stream()
       .collect(Collectors.groupingBy(Employee::getStatus, Collectors.groupingBy((e) -> {
        if(e.getAge() >= 60)
         return "老年";
        else if(e.getAge() >= 35)
         return "中年";
        else
         return "成年";
       })));
      
      System.out.println(map);
     }
     
     //分區(qū)
     @Test
     public void test7(){
      Map<Boolean, List<Employee>> map = emps.stream()
       .collect(Collectors.partitioningBy((e) -> e.getSalary() >= 5000));
      
      System.out.println(map);
     }
     
     //
     @Test
     public void test8(){
      String str = emps.stream()
       .map(Employee::getName)
       .collect(Collectors.joining("," , "----", "----"));
      
      System.out.println(str);
     }
     
     @Test
     public void test9(){
      Optional<Double> sum = emps.stream()
       .map(Employee::getSalary)
       .collect(Collectors.reducing(Double::sum));
      
      System.out.println(sum.get());
     }
    }

    posted on 2018-03-06 08:40 長春語林科技 閱讀(508) 評論(0)  編輯  收藏 所屬分類: java8
    <2018年3月>
    25262728123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

     

    長春語林科技歡迎您!

    常用鏈接

    留言簿(6)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    相冊

    收藏夾

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 69xx免费观看视频| 国产性生交xxxxx免费| 亚洲永久在线观看| 免费在线观看黄色毛片| 国产99视频精品免费专区| 久久亚洲精品国产精品婷婷| 免费人成网站在线播放| 久久国产乱子伦精品免费看| 亚洲精品午夜国产va久久| 国产日产亚洲系列最新| 国产人在线成免费视频| 国产免费伦精品一区二区三区 | 亚洲国产人成中文幕一级二级| 一个人看的www免费视频在线观看 一个人免费视频观看在线www | 亚洲午夜国产精品无码老牛影视 | 四虎影视永久免费观看| 久久永久免费人妻精品| 亚洲AV日韩AV永久无码色欲| 亚洲国产精品SSS在线观看AV| 午夜视频在线观看免费完整版| 国产成年无码久久久免费| 亚洲s码欧洲m码吹潮| 久久亚洲精精品中文字幕| 亚洲国产精品国产自在在线| 黄色网址免费观看| 黄色免费在线网站| 美女被爆羞羞网站在免费观看 | 一区二区三区免费视频网站| 亚洲另类图片另类电影| 亚洲精品无码AV人在线播放| 国产极品美女高潮抽搐免费网站| 57pao一国产成视频永久免费| 高清免费久久午夜精品| 亚洲乱亚洲乱妇24p| 亚洲日韩中文字幕| 国产亚洲欧洲精品| 亚洲中文字幕无码爆乳av中文| 午夜小视频免费观看| aa级一级天堂片免费观看| 8x8×在线永久免费视频| 亚洲午夜av影院|