java8新特性function和lambda深度解析

继续java8新亮点的源码之路,functional interface是一个跳不过的坎,它与lambda的结合使用非常普遍。java.util.function包对于每一个java工程师来说是必备技能,也是最基础的能力,一定要掌握。

head-icon

函数编程的最直接的表现在于将函数作为数据自由传递,结合泛型推导能力使代码表达能力获得飞一般的提升。同时Lambda表达式让你能够将函数作为方法参数或者将代码作为数据对待,让你发现“行级代码”优美。

java8引入新的注解,@FunctionalInterface

函数式注解@FunctionalInterface添加在一个接口上,主要是编译器检查提示作用。

  1. 注解的作用是检测自定义functional接口是否符合要求,编译器会有错误提示;
  2. 一个接口符合functional的要求,不加这个注解也可以正常使用,建议都加上
  3. 有且只能有一个抽象方法但可以有多个非抽象方法,简单说就是接口里面default和static的方法是可以有多个的,其他的方法只能有一个。

lambda表达式写法及注意点

格式:
( parameters ) -> { statements; }

  1. 不需要声明参数类型,编译器可以识别参数值;
  2. 单个参数和语句下,圆括弧和大括弧可以省略;
  3. 表达式是一个闭包,定义了行内执行的方法类型接口;
  4. 只能引用标记了final的外层局部变量,不能在表达式内部修改定义在域外的局部变量,否则会编译错误;
  5. 表达式当中不允许声明一个与局部变量同名的参数或者局部变量;

写法示例:

1@FunctionalInterface 2public interface IPerson { 3 4 String say(String input); 5 //void stand(); 只能有一个抽象方法,不然编译无法默认识别调用 6 7 static void run(String xx){ 8 PrintUtil.printTest("IPerson run : " + xx); 9 } 10 11 static void walk(){ 12 PrintUtil.printTest("IPerson walk"); 13 } 14 15 default void eat(int a, int b){ 16 PrintUtil.printTest("IPerson eat : " + a + " - " + b); 17 } 18} 19 20//当你这种写法是编译器会提示你用lambda 21IPerson person = new IPerson() { 22 @Override 23 public String say(String input) { 24 return "My said is " + input; 25 } 26}; 27PrintUtil.printTest(person.say("i love china.")); 28 29//lambda写法 30IPerson person2 = a -> "My said is " +a; 31PrintUtil.printTest(person2.say("i love china.")); 32//结果是一样的,My said is i love china.

function包中重要接口源码分析

java8function

Consumer,接收一个输入参数T类型并不没有返回值;andThen看源码可以知道是添加一个其后执行的Consumer对象。

这个接口很简单不需要什么解释,看源码一眼OK。

1@FunctionalInterface 2public interface Consumer<T> { 3 void accept(T t); 4 5 default Consumer<T> andThen(Consumer<? super T> after) { 6 Objects.requireNonNull(after); 7 return (T t) -> { accept(t); after.accept(t); }; 8 } 9}

Function,接收一个T类型参数,返回一个R类型的结果。需要注意的是compose\andThen的传入参数和范围参数规则不同,这里的参数类型稍有不慎就会出错,复杂的链路里面排查bug是非常麻烦的事。

1@FunctionalInterface 2public interface Function<T, R> { 3 4 R apply(T t); 5 //生成了function的参数类型同before一样 6 default <V> Function<V, R> compose(Function<? super V, ? extends T> before) { 7 Objects.requireNonNull(before); 8 return (V v) -> apply(before.apply(v)); 9 } 10 //新生成的function的返回值类型要after一样 11 default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) { 12 Objects.requireNonNull(after); 13 return (T t) -> after.apply(apply(t)); 14 } 15 // 16 static <T> Function<T, T> identity() { 17 return t -> t; 18 } 19}

测试用例:

1Function<Integer,String> function = a -> "== " + a; 2PrintUtil.printTest(function.apply(101)); 3Function<String,Boolean> function1 = c -> c.length()>2; 4PrintUtil.printTest(function1.apply("1a")); 5 6PrintUtil.printTest(function.andThen(function1).apply(111)); 7//andThen类似consumer,是前一个function执行后结果作为参数传新生成的function执行,结构:true 8 9Function<Integer,Integer> function2 = c -> c*c; 10//compose和andThen正好逻辑相反,传入的参数function先执行后范围结果作为参数传给新生成的function执行 11PrintUtil.printTest(function.compose(function2).apply(2)); 12//先执行function2,返回结果作为参数再执行function,结果:== 4 13PrintUtil.printTest(function.compose(function2).andThen(function1).apply(2)); 14//先执行function2,其次执行funciton,最后执行function1,结果:true 15PrintUtil.printTest(function2.compose(function2).apply(2)); 16//先执行第二个function2,返回结果作为参数再执行第一个function2,结果:16 17//递归的实现又多了种办法 18 19Function<String,String> function3 = Function.identity();//static方法 20PrintUtil.printTest(function3.apply("hello")); 21//identity定义了一个只返回输入参数的function,结果:hello

confuse

Predicate,是一个条件判断接口,接收一个T参数范围boolean值,默认抽象方法test(t);and\or\negate分别对应逻辑与、或、非操作,isEqual。

1@FunctionalInterface 2public interface Predicate<T> { 3 boolean test(T t); 4 //逻辑与 5 default Predicate<T> and(Predicate<? super T> other) { 6 Objects.requireNonNull(other); 7 return (t) -> test(t) && other.test(t); 8 } 9 //获取该对象否定的Predicate,相当于逆转boolean 10 default Predicate<T> negate() { 11 return (t) -> !test(t); 12 } 13 //逻辑或 14 default Predicate<T> or(Predicate<? super T> other) { 15 Objects.requireNonNull(other); 16 return (t) -> test(t) || other.test(t); 17 } 18 //生成一个判断对象是否相等的Predicate 19 static <T> Predicate<T> isEqual(Object targetRef) { 20 return (null == targetRef) 21 ? Objects::isNull 22 : object -> targetRef.equals(object); 23 } 24}

测试用例:

1Predicate<ICar> carHas4Wheel = a -> a.getWheelCount() > 3; 2ICar weilai = new WeiLaiCar(3); 3 4PrintUtil.printTest(carHas4Wheel.test(weilai)); 5//false 6PrintUtil.printTest(carHas4Wheel.negate().test(weilai)); 7//true 8PrintUtil.printTest(carHas4Wheel.and(b -> b.getWheelCount()>2).test(weilai)); 9//false 10PrintUtil.printTest(carHas4Wheel.and(b -> b.getWheelCount()>2).test(new WeiLaiCar(4))); 11//true 12PrintUtil.printTest(carHas4Wheel.or(b -> b.getWheelCount()>2).test(new WeiLaiCar(3))); 13//true 14 15PrintUtil.printTest(Predicate.isEqual(weilai).test(weilai)); 16//true 17PrintUtil.printTest(Predicate.isEqual(weilai).test(new WeiLaiCar(3))); 18//false

Supplier,是一个只有返回没有参数的接口,只有一个方法get。

1@FunctionalInterface 2public interface Supplier<T> { 3 /** 4 * Gets a result. 5 * @return a result 6 */ 7 T get(); 8}

测试用例:

1Supplier<Integer> supplier = () -> 1 ; 2PrintUtil.printTest(supplier.get()); 3//1 4Supplier<ICar> carSupplier = () -> new WeiLaiCar(5) ; 5PrintUtil.printTest(carSupplier.get() + " : " + carSupplier.get().getWheelCount()); 6//com.ts.util.optional.WeiLaiCar@32a1bec0 : 5

holdOn

Consumer<T> Predicate<T> Supplier<T> Function<T, R> BiFunction<T, U, V>,这里的T U R V并没有特别的定义只是一种约定,就像驼峰命名和泛型中K V E一样。

分享连接

除了介绍的几种函数式接口外,java8在这几个基础上封装了很多的延伸接口,如BiConsumer\ DoubleConsumer\ IntConsumer\ LongConsumer\ ObjIntConsumer等。平时写代码时注意积累,闲的时候多去看看API,慢慢的就掌握了。

Lambda和Functional的结合很大一点就是代码简洁了,看着非常的赏心悦目。JAVA一个纯面向对象的语言,在行级代码上一直是非常的简洁易于调试,而这两个新特性的出现让行级代码的复杂度急剧提升。

相关用例代码已托管Github:play-java-sample

推荐关注

作者:Owen Jia。
推荐关注他的博客:Owen Blog

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Java日期时间API系列31

  时间戳是指格林威治时间1970年01月01日00时00分00秒起至现在的总毫秒数,是所有时间的基础,其他时间可以通过时间戳转换得到。Java中本来已经有相关获取时间戳的方法,Java8后增加新的类Instant等专用于处理时间戳问题。 1获取时间戳的方法和性能对比1.1获取时间戳方法Java8以前

Lambda表达式中Collections的接口有哪些变化?

我们先从最熟悉的\Java集合框架(JavaCollectionsFramework,JCF)\开始说起。为引入Lambda表达式,Java8新增了java.util.function包,里面包含常用的函数接口,这是Lambda表达式的基础,Java集合框架也新增部分接口,以便与Lambda表达式对接。首先回顾一下Java集合框架的接口继承

Java8特性

Java8又称jdk1.8。主要新特性:Lambda表达式 −Lambda允许把函数作为一个方法的参数(函数作为参数传递进方法中。方法引用 −方法引用提供了非常有用的语法,可以直接引用已有Java类或对象(实例)的方法或构造器。与lambda联合使用,方法引用可以使语言的构造更紧凑简洁,减少冗余代码。