美文网首页
JDK8学习总结第零篇:了解Lambda

JDK8学习总结第零篇:了解Lambda

作者: codeMover | 来源:发表于2020-11-02 19:28 被阅读0次

    JDK8

    Java8可谓Java语言历史上变化最大的一个版本,其承若要调整Java编程向着函数式风格迈进,这有助于编写出更为简洁、表达力更强,并且在很多情况下能够利用并行硬件的代码。

    函数式接口(FunctionalInterface)

    函数式接口(Functional Interface)就是一个有且仅有的一个抽象方法,但可以有多个非抽象方法的接口。
    根据Javadoc文档中关于函数式接口有以下特性:

    1. 一个函数式接口只有一个确切的抽象方法。(Conceptually, a functional interface has exactly one abstract method.)
    2. 如果一个类型被注解函数式接口,那么编译器就会要求按照函数式接口的定义要求该函数。(if a type is annotated with annotation type, compilers are required to generate an error message unless: the typre is an interface type and not an annotation type, enum, or class; the annotated type satisfies the requirements of a functional interface. )
    3. 如果某个接口只有一个抽象方法,但我们没有给改接口声明FunctionalInterface注解,那么编译器依旧会将改接口看做是函数式接口。(However, the compiler will treat any interface meetiong the definition od functional interface as functional interface regardless of whether or not a annotation id present on the interface declaration.)
    4. jdk8中默认方法不作为抽象方法个数。(Since default methods hane an implementation, they not abstrace.)
    5. 如果声明了FunctionalInterface注解中重写了java.lang.Object公共方法,那么这个方法不计算抽象方法个数。(If an interface declares an abstract method overriding one of yhe public method of java.lang.Object, that also does not count toward the interface's abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere.)
    函数式接口示例
    @FunctionalInterface
    public interface FunctionalInterfaceTest1 {
    
        /**
         * 接口中抽象方法
         */
        void test();
    
        /**
         * 重写了Object里方法,不作为抽象函数个数
         *
         * @param obj
         * @return
         */
        @Override
        boolean equals(Object obj);
    
        /**
         * 打印接口名
         */
        default void printInterfaceName() {
            System.out.println(this.getClass().getName());
        }
    
    }
    

    Lambda表达式

    Lambda表达式: 在JDK8之前,Java是不支持函数式编程的,所谓的函数编程,即可理解是将一个函数(也称为“行为”)作为一个参数进行传递。通常我们提及得更多的是面向对象编程,面向对象编程是对数据的抽象(各种各样的POJO类),而函数式编程则是对行为的抽象(将行为作为一个参数进行传递)。在JavaScript中这是很常见的一个语法特性,但在Java中将一个函数作为参数传递这却行不通,好在JDK8的出现打破了Java的这一限制。Lambda必须依附函数式接口。

    Lambda表达式作用
    • 传递行为,而不仅仅是值
    • 提升抽象层次
    • API重用性更好
    • 更加灵活
    Lambda结构
    • 一个lambda表达式可以有零个或多个参数
    • 参数的类型既可以明确声明,也可以根据上下文来推断。
    • 所有参数需要包含在圆括号内,参数之间用逗号相隔
    • 空括号代表参数集为空
    • 当只有一个参数,且类型可以推倒,圆括号可以省略
    • 如果lamdba表达式的主体只有一条语句,花括号可以省略。匿名函数的返回类型与该主体表达式一致
    Lambda示例
    (Type1 arg1,Type2 arg2)->{body}
    
    public class LambdaTest1 {
    
        public static void main(String[] args) {
            Thread t1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println("thread t1");
                }
            });
            t1.start();
    
            Thread t2 = new Thread(() -> {System.out.println("thread t2");});
            t2.start();
            
            Thread t3 = new Thread(() -> System.out.println("thread t3"));
            t3.start();
        }
    }
    

    小结

    • 本篇了解到了函数式接口和Lambda表达式概念、特性及基本使用。
    • 下一篇学习常见的函数式接口。

    相关文章

      网友评论

          本文标题:JDK8学习总结第零篇:了解Lambda

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