乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      lambda表達(dá)式Stream流使用

       印度阿三17 2019-08-30

      #Lambda表達(dá)式

      Lambda允許把函數(shù)作為一個(gè)方法的參數(shù)(函數(shù)作為參數(shù)傳遞進(jìn)方法中)


      #lambda表達(dá)式本質(zhì)上就是一個(gè)匿名方法。比如下面的例子:

      public int add(int x, int y) {
          return x   y;
      }
      

      轉(zhuǎn)成Lambda表達(dá)式后是這個(gè)樣子:

      (int x, int y) -> x   y;
      

      參數(shù)類型也可以省略,Java編譯器會根據(jù)上下文推斷出來:

      (x, y) -> x   y; //返回兩數(shù)之和
      

      或者

      (x, y) -> { return x   y; } //顯式指明返回值
      

      #java 8 in Action這本書里面的描述:

      A lambda expression can be understood as a concise representation of an anonymous function
      that can be passed around: it doesn’t have a name, but it has a list of parameters, a body, a
      return type, and also possibly a list of exceptions that can be thrown. That’s one big definition;
      

      #格式:

      (x, y) -> x   y;
      -----------------
      (x,y):參數(shù)列表
      x y  : body
      

      #Lambda表達(dá)式用法實(shí)例:

      package LambdaUse;
      
      /**
       * @author zhangwenlong
       * @date 2019/8/25 15:17
       */
      public class LambdaUse {
      
          public static void main(String[] args) {
      
              //匿名類實(shí)現(xiàn)
              Runnable r1 = new Runnable() {
                  @Override
                  public void run() {
                      System.out.println("Hello world");
                  }
              };
              //Lambda方式實(shí)現(xiàn)
              Runnable r2 = ()-> System.out.println("Hello world");
      
              process(r1);
              process(r2);
              process(()-> System.out.println("Hello world"));
          }
          private  static void  process(Runnable r){
              r.run();
          }
      }
      
      

      輸出的結(jié)果:

      Hello world
      Hello world
      Hello world
      

      FunctionalInterface注解修飾的函數(shù)

      #predicate的用法

      package LambdaUse;
      
      import java.applet.Applet;
      import java.util.ArrayList;
      import java.util.Arrays;
      import java.util.List;
      import java.util.function.LongPredicate;
      import java.util.function.Predicate;
      
      /**
       * @author zhangwenlong
       * @date 2019/8/25 15:30
       */
      public class PredicateTest {
      
          //通過名字(Predicate實(shí)現(xiàn))
          private static List<Apple> filterApple(List<Apple> list, Predicate<Apple> predicate){
                  List<Apple> appleList = new ArrayList<>();
                  for(Apple apple : list){
                      if(predicate.test(apple)){
                          appleList.add(apple);
                      }
                  }
                  return appleList;
          }
          //通過重量(LongPredicate實(shí)現(xiàn))
          private static List<Apple> filterWeight(List<Apple> list, LongPredicate predicate){
              List<Apple> appleList = new ArrayList<>();
              for(Apple apple : list){
                  if(predicate.test(apple.getWeight())){
                      appleList.add(apple);
                  }
              }
              return appleList;
          }
      
          public static void main(String[] args) {
      
              List<Apple> list = Arrays.asList(new Apple("蘋果",120),new Apple("香蕉",110));
              List<Apple> applelist = filterApple(list,apple -> apple.getName().equals("蘋果"));
              System.out.println(applelist);
              List<Apple> appleList = filterWeight(list, (x) -> x > 110);
              System.out.println(appleList);
          }
      }
      
      
      

      執(zhí)行結(jié)果:

      [Apple{name='蘋果', weight=120}]
      [Apple{name='蘋果', weight=120}]
      

      #Lambda表達(dá)式的方法推導(dǎo)

      package LambdaUse;
      
      import java.util.function.Consumer;
      
      /**
       * @author zhangwenlong
       * @date 2019/8/25 16:02
       */
      public class MethodReference {
      
          private static <T> void useConsumer(Consumer<T> consumer,T t){
              consumer.accept(t);
          }
      
          public static void main(String[] args) {
              //定義一個(gè)匿名類
              Consumer<String> stringConsumer = (s) -> System.out.println(s);
              useConsumer(stringConsumer,"Hello World");
              //lambda
              useConsumer((s) -> System.out.println(s),"ni hao");
              //Lambda的方法推導(dǎo)
              useConsumer(System.out::println,"da jia hao");
          }
      }
      

      執(zhí)行結(jié)果:

      Hello World
      ni hao
      da jia hao
      

      參數(shù)推導(dǎo)其他例子:

      package LambdaUse;
      
      import java.net.Inet4Address;
      import java.util.function.BiFunction;
      import java.util.function.Function;
      
      /**
       * @author zhangwenlong
       * @date 2019/8/25 16:53
       */
      public class paramterMethod {
      
          public static void main(String[] args) {
              //情況1:A method reference to a static method (for example, the method parseInt of Integer, written Integer::parseInt)
              //靜態(tài)方法
              //常用的使用
              Integer value = Integer.parseInt("123");
              System.out.println(value);
              //使用方法推導(dǎo)
              Function<String,Integer> stringIntegerFunction = Integer::parseInt;
              Integer apply = stringIntegerFunction.apply("123");
              System.out.println(apply);
              //情況2:A method reference to an instance method of an arbitrary type (for example, the method length of a String, written String::length)
              //對象的方法
              String ss = "helloWorld";
              System.out.println(ss.charAt(3));
              //推導(dǎo)方法
              BiFunction<String,Integer,Character> stringIntegerCharacterBiFunction = String::charAt;
              System.out.println(stringIntegerCharacterBiFunction.apply(ss,3));
      
              //情況3:構(gòu)造函數(shù)方法推導(dǎo)
              //常用方式
              Apple apple = new Apple("蘋果",120);
              System.out.println(apple);
              //推導(dǎo)方式
              BiFunction<String,Integer,Apple> appleBiFunction = Apple::new;
              System.out.println(appleBiFunction.apply("蘋果",120));
          }
      }
      

      執(zhí)行結(jié)果:

      123
      123
      l
      l
      Apple{name='蘋果', weight=120}
      Apple{name='蘋果', weight=120}
      

      具體的詳細(xì)解釋可以參考:

      方法推導(dǎo)詳解

      #Stream(流以及一些常用的方法)
      特點(diǎn):并行處理
      例子:

      package StreamUse;
      
      import java.util.*;
      import java.util.stream.Collectors;
      
      /**
       * @author zhangwenlong
       * @date 2019/8/25 17:20
       */
      public class StreamTest {
      
          public static void main(String[] args) {
              List<Apple> appleList = Arrays.asList(
                      new Apple("蘋果",110),
                      new Apple("桃子",120),
                      new Apple("荔枝",130),
                      new Apple("香蕉",140),
                      new Apple("火龍果",150),
                      new Apple("芒果",160)
              );
              System.out.println(getNamesByCollection(appleList));
              System.out.println(getNamesByStream(appleList));
          }
      
          //collection實(shí)現(xiàn)查詢重量小于140的水果的名稱
          private static List<String> getNamesByCollection(List<Apple> appleList){
              List<Apple> apples = new ArrayList<>();
      
              //查詢重量小于140的水果
              for(Apple apple : appleList){
                  if(apple.getWeight() < 140){
                     apples.add(apple);
                  }
              }
              //排序
              Collections.sort(apples,(a,b)->Integer.compare(a.getWeight(),b.getWeight()));
      
              List<String> appleNamesList = new ArrayList<>();
              for(Apple apple : apples){
                  appleNamesList.add(apple.getName());
              }
              return  appleNamesList;
          }
      
          //stream實(shí)現(xiàn)查詢重量小于140的水果的名稱
          private static List<String> getNamesByStream(List<Apple> appleList){
              return  appleList.stream().filter(d ->d.getWeight() < 140)
                      .sorted(Comparator.comparing(Apple::getWeight))
                      .map(Apple::getName)
                      .collect(Collectors.toList());
          }
      }
      
      
      來源:https://www./content-4-425151.html

        本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多