Java的lambda和函数式编程

一、lambda出现的背景

  • 精简代码
Runnable runnable1 = new Runnable() {
    @Override
    public void run() {
        System.out.println("runned111");
    }
};
Runnable runnable = ()->{
    System.out.println("runned0");
};
  • 实现回调函数的特点
    我们知道在JavaScript中,一个函数是可以作为参数传入另一个函数的,这就为一种“即用即走,低耦合”的思想提供了很好的实现
TestStd<String> std = new TestStd(Arrays.asList("1","1","1","1","124","124","124","124"));
// 可以看做回调函数
Function<String,Integer> function = (v1)->Integer.parseInt(v1);
List<Integer> integers2 = std.map2(function);
基本语法

(变量名)—>{方法体}
当变量名为一个时可省略(),当方法体为一句时可省略{}

组成部分

lambda的语法常用的有接口式,Function式和MethodReference种。

二、接口式Lambda的使用

顾名思义,接口式Lambda即将一个lambda看做一个接口的实现,例:

@FunctionalInterface
interface Tniusiji {
    String get(String v1);
}

public String get(Tniusiji tniusiji){
    return tniusiji.get();
}

System.out.println(std.get(()->"123"));

注意:
* @FunctionalInterface 可选的,添加上之后用于检验该接口为”函数式接口”
* “函数式接口”是指仅仅只包含一个”抽象”方法的接口,也即可以有默认/静态方法(参见Consumer,Function)

三、Function,Consumer,Predict等

A. Consumer

方法体

@FunctionalInterface
public interface Consumer<T> {

    /**
     * Performs this operation on the given argument.
     *
     * @param t the input argument
     */
    void accept(T t);

即Consumer的调用方法是不需要有返回值的,所以我们可以自己写一个Consumer尝试一下

// 定义部分
private List<T> beans = null;
public void foreach(Consumer<? super T> consumer){
    for (int i=0;i<beans.size();i++){
        consumer.accept(beans.get(i));
    }
}

// 使用
TestStd<String> std = new TestStd(Arrays.asList("1","1","1","1","124","124","124","124"));
std.foreach(s->{
    System.out.println("print: "+s);
});
B. Function

方法体

@FunctionalInterface
public interface Function<T, R> {

    /**
     * Applies this function to the given argument.
     *
     * @param t the function argument
     * @return the function result
     */
    R apply(T t);

即如果定义Function类型的Lambda是需要有返回值的,所以我们可以自己写一个Function尝试一下

public<R> List<R> map2(Function<T,R> function){
    List<R> rList = new ArrayList<>();
    foreach(s->{
        rList.add(function.apply(s));
    });
    return rList;
}

四、Method Reference

在使用Function类的接口时,可以使用 类名::方法名 的形式进行调用

// 两者等价
List<Integer> integers = std.map2(v1->Integer.parseInt(v1));
List<Integer> integers3 = std.map2(Integer::parseInt);
// 两者等价
std.foreach(System.out::println);
std.foreach(s->System.out.println(s));

使用Method Reference有四种方式

构造器引用:它的语法是Class::new,或者更一般的Class< T >::new实例如下:

final Car car = Car.create( Car::new );
final List< Car > cars = Arrays.asList( car );
静态方法引用:它的语法是Class::static_method,实例如下:

cars.forEach( Car::collide );
特定类的任意对象的方法引用:它的语法是Class::method实例如下:

cars.forEach( Car::repair );
特定对象的方法引用:它的语法是instance::method实例如下:

final Car police = Car.create( Car::new );
cars.forEach( police::follow );
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值