1 Function<T, R>中的T, R表示接口输入、输出的数据类型。

  • R apply(T t)

  • apply
  • .例子:func是定义好的Function接口类型的变量,他的输入、输出都是Integer类型,调用calculate方法时,将func作为参数传入,对参数5进行处理。

    FunctionTest functionTest = new FunctionTest();
    // return e + 5;就是apply方法的具体实现
    Function<Integer, String> func = e -> {return String.valueOf(e + 6);};
    String result = functionTest.calculate(5, func);
    System.out.println(result);

    public String calculate(Integer a, Function<Integer, String> function) {
    return function.apply(a);
    }

  • andThen:

    • 先处理参数,再对返回值使用操作after进行处理。
      Function<Integer, Integer> func = e -> {return e + 5;};
      Function<Integer, Integer> func2 = e -> {return e * 5;};
      //func2即after
      func.andThen(func2).apply(5); // 50

    compose:

    • andThen刚好相反:先使用操作before处理参数,再对返回值进行处理。
      Function<Integer, Integer> func = e -> {return e + 5;};
      Function<Integer, Integer> func2 = e -> {return e * 5;};
      //func2即before
      func.compose(func2).apply(5); // 30
    • compose源码:
      default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
      Objects.requireNonNull(before);
      return (V v) -> apply(before.apply(v));//第一个apply是调用当前接口的方法
      }
    • 注意compose方法的返回值依然是Function<T, R>类型,所以不是
      return this.apply(before.apply(v));

    案例:

  •  public class FunctionTest2 {
    public static void main(String[] args) {
    FunctionTest2 functionTest2 = new FunctionTest2();
    int result1 = functionTest2.compute(5, e -> e * 5, e -> e + 5);
    int result2 = functionTest2.compute2(5, e -> e * 5, e -> e + 5);
    int result3 = functionTest2.compute3(5, e -> e * 5, e -> e + 5);
    int result4 = functionTest2.compute4(5, e -> e * 5, e -> e + 5);
    System.out.println(result1);//50
    System.out.println(result2);//30
    System.out.println(result3);//130
    System.out.println(result4);//250
    }

    public int compute(int source, Function<Integer, Integer> function1, Function<Integer, Integer> function2) {
    return function1.compose(function2).apply(source);
    }
    public int compute2(int source, Function<Integer, Integer> function1, Function<Integer, Integer> function2) {
    return function1.andThen(function2).apply(source);
    }
    public int compute3(int source, Function<Integer, Integer> function1, Function<Integer, Integer> function2) {
    return function1.andThen(function2).compose(function1).apply(source); //从后往前 25 125 130
    }
    public int compute4(int source, Function<Integer, Integer> function1, Function<Integer, Integer> function2) {
    return function1.compose(function2).andThen(function1).apply(source); } //10*5 50*5
    }

JDK8新特性 -- Function接口: apply,andThen,compose的更多相关文章

  1. JDK8新特性之接口

    在JDK7及以前的版本中,接口中都是抽象方法,不能定义方法体,但是从jdk8开始,接口中可以定义静态的非抽象的方法,直接使用接口名调用静态方法,但是它的实现类的类名或者实例却不可以调用接口中的静态方法 ...

  2. JDK8新特性:接口的静态方法和默认方法

    在jdk8之前,interface之中可以定义变量和方法,变量必须是public.static.final的,方法必须是public.abstract的.由于这些修饰符都是默认的,所以在JDK8之前, ...

  3. JDK8新特性之接口默认方法与静态方法

    接口默认方法与静态方法 有这样一些场景,如果一个接口要添加一个方法,那所有的接口实现类都要去实现,而某些实现类根本就不需要实现这个方法也要写一个空实现,所以接口默认方法就是为了解决这个问题. 接口静态 ...

  4. jdk8新特性--函数式接口的使用

    函数式接口的概念: 函数式接口的格式: 示例: 函数式接口的使用: 简化lambda表达式:

  5. JDK8新特性:使用stream、Comparator和Method Reference实现集合的优雅排序

    大家对java接口Comparator和Comparable都不陌生,JDK8里面Comparable还和以前一样,没有什么改动:但是Comparator在之前基础上增加了很多static和defau ...

  6. JDK8 新特性

    JDK8 新特性目录导航: Lambda 表达式 函数式接口 方法引用.构造器引用和数组引用 接口支持默认方法和静态方法 Stream API 增强类型推断 新的日期时间 API Optional 类 ...

  7. JDK1.8新特性——Collector接口和Collectors工具类

    JDK1.8新特性——Collector接口和Collectors工具类 摘要:本文主要学习了在Java1.8中新增的Collector接口和Collectors工具类,以及使用它们在处理集合时的改进 ...

  8. JDK8新特性一览

    转载自:http://blog.csdn.net/qiubabin/article/details/70256683 官方新特性说明地址 Jdk8新特性.png 下面对几个常用的特性做下重点说明. 一 ...

  9. 一次电话Java面试的问题总结(JDK8新特性、哈希冲突、HashMap原理、线程安全、Linux查询命令、Hadoop节点)

    面试涉及问题含有: Java JDK8新特性 集合(哈希冲突.HashMap的原理.自动排序的集合TreeSet) 多线程安全问题 String和StringBuffer JVM 原理.运行流程.内部 ...

随机推荐

  1. [csdn markdown]使用摘记一源码高亮及图片上传和链接

    本文主要内容是体验csdn markdown的代码块高亮显示和图片链接及上传. 图片上传 上边这是标题行.仅仅须要使用一个#就能够表示.几个表示是几级标题 图片上传 本地图片上传控件 本地图片上传方式 ...

  2. 【树状数组】POJ 2155 Matrix

    附一篇经典翻译,学习 树状数组  http://www.hawstein.com/posts/binary-indexed-trees.html /** * @author johnsondu * @ ...

  3. 【Unity3D】 KeyCode 键码

    Key codes returned by Event.keyCode. These map directly to a physical key on the keyboard. KeyCode是由 ...

  4. ACdream 1112 Alice and Bob (博弈&amp;&amp;素数筛选优化)

    题目链接:传送门 游戏规则: 没次能够将一堆分成两堆 x = a*b (a!=1&&b!=1)x为原来堆的个数,a,b为新堆的个数. 也能够将原来的堆的个数变成原来堆的约数y.y!=x ...

  5. PHP项目的设计过程

    过程说明: 1)产品部依据需求设计出原型图和需求文档. 2)产品部和需求方与技术一起过一遍需求. 这样能够让需求方确认需求:和所參与的技术(设计部,制作部,php,測试部等)对要设计的产品有一个大致的 ...

  6. UNIX环境高级编程之第3章:文件I/O

    3.1 引言 文件I/O函数:打开文件,读文件,写文件 经常使用到五个函数:open, read, write, lseek, close. 本章描写叙述的函数都是:不带缓冲的I/O(unbuffer ...

  7. SpringMVC高速实现文件上传功能

    SpringMVC为我们封装了上传文件的功能,如今就试用一下 须要的jar包 我用的是Maven项目,就不须要到处下载Jar包了 SpringMVC的搭建 首先要在applicationContext ...

  8. poj 1840(五元三次方程组)

    Description Consider equations having the following form: a1x1 3+ a2x2 3+ a3x3 3+ a4x4 3+ a5x5 3=0 T ...

  9. 杂项-Java:JNI

    ylbtech-杂项-Java:JNI JNI是Java Native Interface的缩写,它提供了若干的API实现了Java和其他语言的通信(主要是C&C++).从Java1.1开始, ...

  10. c++ string 解析ip

    比如输入是192.168.80.12-15,解析成192.168.80.12.192.168.80.13.192.168.80.14.192.168.80.15. #include <iostr ...