java中方法支持lambda_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的好处

支持函数式编程,我们在编程上多一种编程模式选择,对于一些喜欢这种编程方式的人是个福音

使用lambda的地方,往往代码会精简很多,看起来不臃肿,易读,有逼格

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

lambda在Java8中的使用

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

List 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

(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 functional interface 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

* not 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.

*

*

Note that instances of functional interfaces can be created with

* lambda expressions, method references, or constructor references.

*

*

If a type is annotated with this annotation type, compilers are

* required to generate an error message unless:

*

*

*

The type is an interface type and not an annotation type, enum, or class.

*

The annotated type satisfies the requirements of a functional interface.

*

*

*

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 {}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值