热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

JDK8新特性java.util.functionFunction接口

14年,Oracle公司如期发布了Java8正式版。现如今4年过去了,终于鼓起勇气认真对待它,就好似虽然认识了好几年的伙伴,

14年,Oracle公司如期发布了Java 8正式版。现如今4年过去了,终于鼓起勇气认真对待它,就好似虽然认识了好几年的伙伴,突然感觉要成为情侣的感觉…… 
JDK 1.8 API包含了很多内建的函数式接口,在老Java中常用到的比如Comparator或者Runnable接口,这些接口都增加了@FunctionalInterface注解以便能用在lambda上。现如今,我们则从Function常用函数入口,真正了解一下。

nametypedescription
ConsumerConsumer接收T对象,不返回值
PredicatePredicate接收T对象并返回boolean
FunctionFunction接收T对象,返回R对象
SupplierSupplier提供T对象(例如工厂),不接收值
UnaryOperatorUnaryOperator接收T对象,返回T对象
BinaryOperatorBinaryOperator接收两个T对象,返回T对象

标注为FunctionalInterface的接口被称为函数式接口,该接口只能有一个自定义方法,但是可以包括从object类继承而来的方法。如果一个接口只有一个方法,则编译器会认为这就是一个函数式接口。是否是一个函数式接口,需要注意的有以下几点:

  • 该注解只能标记在”有且仅有一个抽象方法”的接口上。
  • JDK8接口中的静态方法和默认方法,都不算是抽象方法。
  • 接口默认继承java.lang.Object,所以如果接口显示声明覆盖了Object中方法,那么也不算抽象方法。
  • 该注解不是必须的,如果一个接口符合”函数式接口”定义,那么加不加该注解都没有影响。加上该注解能够更好地让编译器进行检查。如果编写的不是函数式接口,但是加上了@FunctionInterface,那么编译器会报错。
  • 在一个接口中定义两个自定义的方法,就会产生Invalid ‘@FunctionalInterface’ annotation; FunctionalInterfaceTest is not a functional interface错误.

Function常用方法&&实践

//将Function对象应用到输入的参数上,然后返回计算结果。
R apply(T t);

  • 1
  • 2

//返回一个先执行当前函数对象apply方法再执行after函数对象apply方法的函数对象。
default <V> Function<T, V> andThen(Functionsuper R, ? extends V> after) {Objects.requireNonNull(after);return (T t) -> after.apply(apply(t));}

  • 1
  • 2
  • 3
  • 4
  • 5

//返回一个先执行before函数对象apply方法再执行当前函数对象apply方法的函数对象
default <V> Function<V, R> compose(Functionsuper V, ? extends T> before) {Objects.requireNonNull(before);return (V v) -> apply(before.apply(v));}

  • 1
  • 2
  • 3
  • 4
  • 5

compose 和 andThen 的不同之处是函数执行的顺序不同。compose 函数先执行参数&#xff0c;然后执行调用者&#xff0c;而 andThen 先执行调用者&#xff0c;然后再执行参数。

public static void main(String[] args) {Function name &#61; e -> e * 2;Function square &#61; e -> e * e;int value &#61; name.andThen(square).apply(3);System.out.println("andThen value&#61;" &#43; value);int value2 &#61; name.compose(square).apply(3);System.out.println("compose value2&#61;" &#43; value2);//返回一个执行了apply()方法之后只会返回输入参数的函数对象Object identity &#61; Function.identity().apply("huohuo");System.out.println(identity);}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

直接看结果&#xff1a;

andThen value&#61;36
compose value2&#61;18
huohuo

  • 1
  • 2
  • 3

apply基本应用

字符串长度记录返回

public class MyFunction implements Function<String,Integer>{&#64;Overridepublic Integer apply(String s) {return s.length();}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

返回两个字符串的连接&#xff0c;BiFunction与Function的不同就是传入两个参数&#xff0c;依旧返回一个值。

public class MyBiFunction implements BiFunction<String, String, String> {&#64;Overridepublic String apply(String s, String s2) {return s&#43;";"&#43;s2;}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

最后调用结果&#xff1a;

private static String hello &#61; "Nice to meet you";private static String name &#61; "my name is huohuo";public static void main(String[] args) {MyFunction myFunction &#61; new MyFunction();MyBiFunction biFunction &#61; new MyBiFunction();int num &#61; myFunction.apply(hello);String valueBi &#61; biFunction.apply(hello, name);//hello长度返回System.out.println(num);//语句整合返回System.out.println(valueBi);}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

返回值&#xff1a;

16
Nice to meet you;my name is huohuo

  • 1
  • 2

其实使用的过程感觉这些都无所必要&#xff0c;但是对于新特性以及代码规范而言&#xff0c;即使是简易代码&#xff0c;也有了一个整合的过程。Function简单实践仅此为止&#xff0c;下篇文章讲述Predicate的使用。


推荐阅读
author-avatar
阿什顿安抚_343
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有