美文网首页
第1讲:Lambda表达式初探与函数式接口

第1讲:Lambda表达式初探与函数式接口

作者: Finlay_Li | 来源:发表于2020-07-03 18:36 被阅读0次

    何为lambda表达式

    In programming languages such as
    Lisp, Python and Ruby lambda is an operator
    used to denote anonymous functions or
    closures, following the usage of lambda
    calculus

    为何要lambda表达式

    1. 在java中,我们无法把函数作为参数,传递给另一个方法,也无法声明一个返回"函数" 的方法
    2. 在JavaScript中, 函数作为一个参数,返回值是函数的执行结果。这非常常见,如执行成功的回调; 因此JavaScript 是一门典型的函数式编程语言

    lambda的基本结构

    (param1, param2, param3) -> {
    //TODO......
    }

    • 可以看出:以 -> 进行了划分

    初探从list遍历上手

    package com.finlay.scaffold;
    
    import com.google.common.collect.Lists;
    
    import java.util.ArrayList;
    import java.util.function.Consumer;
    
    /**
     * @author: Finlay
     * @description:
     * @date: 2020-07-07 6:01 下午
     */
    public class Test1 {
    
        /*
         * lambda 初探
         * */
        public static void main(String[] args) {
            ArrayList<Integer> integers = Lists.newArrayList(1, 2, 3, 4, 5);
    
            //第1种遍历
            for (int i = 0; i < integers.size(); i++) {
                System.out.println(i);
            }
            //第2种遍历
            for (Integer i :
                    integers) {
                System.out.println(i);
            }
            //第3种遍历:重点关注Consumer这个函数式接口
            integers.forEach(new Consumer<Integer>() {
                @Override
                public void accept(Integer i) {
                    System.out.println(i);
                }
            });
    
            //第4种遍历
            integers.forEach(i -> System.out.println(i));
        }
    }
    
    

    Consumer分析

    1. 我们可以看到以下这段匿名内部类的实现
            integers.forEach(new Consumer<Integer>() {
                @Override
                public void accept(Integer i) {
                    System.out.println(i);
                }
            });
    
    1. 可以看到 Consumer 的简单构成
    @FunctionalInterface
    public 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); };
        }
    }
    
    1. @FunctionalInterface
      其与普通的接口不同,标注了@FunctionalInterface,我们把其说明摘下来

    /**

    • An informative annotation type used to indicate that an interface
    • type declaration is intended to be a <i>functional interface</i> as
    • defined by the Java Language Specification.
    • Conceptually, a functional interface has exactly one abstract
    • method. Since {@linkplain java.lang.reflect.Method#isDefault()
    • default methods} have an implementation, they are not abstract. If
    • an interface declares an abstract method overriding one of the
    • public methods of {@code java.lang.Object}, that also does
    • <em>not</em> count toward the interface's abstract method count
    • since any implementation of the interface will have an
    • implementation from {@code java.lang.Object} or elsewhere.
    • <p>Note that instances of functional interfaces can be created with
    • lambda expressions, method references, or constructor references.
    • <p>If a type is annotated with this annotation type, compilers are
    • required to generate an error message unless:
    • <ul>
    • <li> The type is an interface type and not an annotation type, enum, or class.
    • <li> The annotated type satisfies the requirements of a functional interface.
    • </ul>
    • <p>However, the compiler will treat any interface meeting the
    • definition of a functional interface as a functional interface
    • regardless of whether or not a {@code FunctionalInterface}
    • annotation is present on the interface declaration.
    • @jls 4.3.2. The Class Object
    • @jls 9.8 Functional Interfaces
    • @jls 9.4.3 Interface Method Body
    • @since 1.8
      */

    由此引出函数式接口的概念:

    • 如果一个接口只有一个抽象方法,那么该接口就是一个函数式接口
    • 如果在接口上声明了@FunctionalInterface,编译器将按函数式接口的定义来要求该接口
    • 如果接口仅只有1个抽象方法时,编译器依旧会将其看做是函数式接口

    相关文章

      网友评论

          本文标题:第1讲:Lambda表达式初探与函数式接口

          本文链接:https://www.haomeiwen.com/subject/nuheqktx.html