Java8 lambda支持

发布时间:2019-11-19 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Java8 lambda支持脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

函数式编程

说lambdas前,先理解下什么是函数式编程,如果你是个纯Java程序员,而且之前一直是没有使用过Java8,可能还没有使用过这种编程方式。用一句最直接的话解释就是可以把函数当做参数传入。举个下面这样的列子

int c1(int x,int y){     return x+y; }  void func(     c1(int x,int y), // 参数一,这里相当于是把c1这个函数直接传进来     int c  // 参数二 ){ // do something ...}

上面的列子只是举个简单例子,Java中并没有这样的语法,下面用Java8的支持的lambdas语法演示下:

 // 在Java8中使用lambdas方式,可以直接这样写: void func((x,y)->{x+y},int y) {// do something...}  // (x,y)->x+y 这样写之前必须有一个这样对应的接口是这样定义的,如下 @FunctionalInterface   // 这个注解不是必须的,只是为了表明这个接口是用于支持Lamdas函数 public interface Func{     int c1(int x,int y);  }  // 在举个使用异步线程的例子 new Thread(()->{// do something}).start() // 这里Runnable对象,就可以用lambdas表达式:()->{do something} // 当代码只有一行的时候,可以不需要{}

至于编译器是怎样解释lambdas的语法的,我们先可以大胆猜测是把它编译成一个匿名的对象,是不是可以这样解释且解释的通,下面具体介绍下

lambda是什么

“Lambda 表达式”(lambda exPression)是一个匿名函数,Lambda表达式基于数学中的λ演算得名,直接对应于其中的lambda抽象(lambda abstraction),是一个匿名函数,即没有函数名的函数。Lambda表达式可以表示闭包(注意和数学传统意义上的不同)。

可以理解为lamdba就是一种表达式语言,就是我们学习数学时,用一些符号来代表一些数学计算表达。

使用lambda的好处

    @H_622_126@支持函数式编程,我们在编程上多一种编程模式选择,对于一些喜欢这种编程方式的人是个福音
  • 使用lambda的地方,往往代码会精简很多,看起来不臃肿,易读,有逼格
  • @H_304_130@

    这是我个人使用后的一个感受

    lambda在Java8中的使用

    lambda是一种表达式语言,那我们常见可用的地方就是在一些数学计算描述中,如集合遍历、排序,或者自定义一些lambda表达式,例如下面用于描述集合排序规则:

    List<String> names = Arrays.asList("peter", "anna", "mike", "xenia”); // (a,b)->a.compareTo(b) 可以这样直接描述比较的规则 Collections.sort(names, (a,b)->a.compareTo(b));

    lambda的用法规则

    怎样编写lambda表达式 ?写法很简单,下面这样描述
    params -> expression  params -> {expression} //在表达式中可以通过::直接调用参数对象拥有的方法,如 a::length Lambda表达式编写时可以自动参数类型,比如上面对names集合排序时,定义类型时List<String> (a,b)->a.compareTo(b) // 此时a,b的类型是String类型,你可以向下面这样指定类型,但是多余的 (String a,String b)->a.compareTo(b) // 不用指定String类型修饰,可以自动推导
    什么时候可以使用lambda表达式?

    Java中新增了一个注解:按照其解释就是说,使用该注解注释的接口都是函数接口,如果接口没有使用该注解声明,也会被当做函数接口。意思就是说,只要是接口类型,我们都可以传入lambda表达式。在java.util.function包下定义了各种函数接口

    /**  * 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  */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface FunctionalInterface {}

    脚本宝典总结

    以上是脚本宝典为你收集整理的Java8 lambda支持全部内容,希望文章能够帮你解决Java8 lambda支持所遇到的问题。

    如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

    本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
    如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。