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 );
上一篇:
IDEA上Java项目控制台中文乱码