java8_01_Lambda

来源:互联网 发布:李兴华java百度云 编辑:程序博客网 时间:2024/06/05 19:05

Java8属于读书笔记系列,版权归《Java 8实战》,请勿商用。欢迎转载!!

  1. Lambda的形式

    • 参数列表 箭头 lambda主体
    • (String s) -> s.length()
    • (String s) -> s.length() > 3

    • () -> 42

    • (String s1,String s2) -> s1.length().compareTo(s2.length());

    • (int x, int y) -> {
      System.out.println(“Result:”);
      System.out.println(x+y);
      }

  2. 在哪里使用Lambda

    1. 函数式接口

      • 定义只有一个抽象方法的接口,可以有任意个默认的实现方法。

      • 接口代码demo:

        @FunctionalInterfacepublic interface Consumer<T> {    void accept(T t);    default Consumer<T> andThen(Consumer<? super T> after) {        Objects.requireNonNull(after);        return (T t) -> { accept(t); after.accept(t);    };}
    2. 函数描述符
      • > 函数式接口的抽象方法的签名基本上就是Lambda表达式的签名。我们将这种抽象方法叫作 函数描述符.
  3. Lambda 实践:环绕执行模式

    • 自定义函数式接口,然后使用.
    //定义一个函数式接口,    @FunctionalInterfacepublic interface BufferedReaderProcessor {    //进行BufferedReader 有关的操作,结果输出String    String process(BufferedReader b) throws IOException ;}public class Test {    //1.行为参数化=》,BufferedReaderProcessor b 是具体的行为。但具体是什么行为,调用方法的时候以传入的参数决定    public static String processFile(String fileLocation,BufferedReaderProcessor b) throws IOException{            BufferedReader br = new BufferedReader(new FileReader(fileLocation));            return b.process(br);    }    public static void main(String[] args) throws IOException {        //2.参数传递的时候,同时定义并传递了行为        String oneLine = processFile("d:/data.txt",(BufferedReader br) -> br.readLine());        String twoLine = processFile("d:/data.txt",(BufferedReader br) -> br.readLine() +";;"+ br.readLine() );        System.out.println(oneLine);        System.out.println(twoLine);    }}
    • java API 定义的函数式接口

      1. Predicate

         // 传入 T t,返回boolean    @FunctionalInterfacepublic interface Predicate<T>{     boolean test(T t);}public class FunctionInterfaceDemo {   public static <T> List<T> filter(List<T> list, Predicate<T> p){    List<T> results = new ArrayList<T>();    for (T t : list) {        if (p.test(t))      // 具体的操作,其实还是个抽象的形式,调用的时候,p就是具体的方法            results.add(t);        }        return results;    }    public static void main(String[] args) {        List<String> strings = Arrays.asList("zs","ls","","wemz"," ");        //声明策略,具体的处理方式        Predicate<String> nonEmptyStringPredicate = (String s) -> !s.isEmpty();        //传入参数并和策略,执行        List<String> nonEmpty = FunctionInterfaceDemo.filter(strings,nonEmptyStringPredicate);    }}
      2. Consumer

        // 传入T t , 没有返回值@FunctionalInterfacepublic interface Consumer<T>{    void accept(T t);} //使用示例:public class FunctionInterfaceDemo {    public static <T> void doSomething(T t, Consumer<T> consumer){        consumer.accept(t);    }    public static void main(String[] args) {        Consumer<String> sayHello = (String s) -> System.out.println( s+",hello !");        //传入参数并执行        FunctionInterfaceDemo.doSomething("zs",sayHello);    }}
      3. Function

        // 传入 T,返回R    @FunctionalInterfacepublic interface Function<T, R>{     R apply(T t);}
      4. Java8中的常用函数式接口

      函数式接口 函数描述符 原始类型特化 Predicate T->boolean IntPredicate,
      LongPredicate,
      DoublePredicate Consumer T->void IntConsumer,
      LongConsumer,
      DoubleConsumer Function T->R IntFunction,
      IntToDoubleFunction,
      IntToLongFunction,
      LongFunction,
      LongToDoubleFunction,
      LongToIntFunction,
      DoubleFunction,
      ToIntFunction,
      ToDoubleFunction,
      ToLongFunction Supplier ()->T BooleanSupplier,
      IntSupplier,
      LongSupplier,
      DoubleSupplier UnaryOperator T->T IntUnaryOperator,
      LongUnaryOperator,
      DoubleUnaryOperator BinaryOperator (T,T)->T IntBinaryOperator,
      LongBinaryOperator,
      DoubleBinaryOperator BiPredicate (L,R)->boolean BiConsumer (T,U)->void ObjIntConsumer,
      ObjLongConsumer,
      ObjDoubleConsumer BiFunction (T,U)->R ToIntBiFunction,
      ToLongBiFunction,
      ToDoubleBiFunction