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. Lifting the Stone 计算几何 多边形求重心

    Problem Description There are many secret openings in the floor which are covered by a big heavy sto ...

  2. 洛谷 P2548 [AHOI2004]智能探险车

    P2548 [AHOI2004]智能探险车 题目描述 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 复制 4 3 sunny plain full many sunny moun ...

  3. systemtap dtrace与 oracle

    https://fritshoogland.wordpress.com/page/3/ http://externaltable.blogspot.com/2013/06/dtrace-explora ...

  4. linux给文件或文件夹加入apache权限

    系统环境:ubuntu11.10/apache2/php5.3.6 在LAMP环境中,測试一个简单的php文件上传功能时,发现/var/log/apache2/error.log中出现例如以下php警 ...

  5. MySql 同一个列中的内容进行批量改动

    问题重现: MySql 数据库中,一给列的内容中包含 ".wmv"     须要将 "." 后的wmv格式 换为"flv" 解决的方法 up ...

  6. 蓝牙调试工具hcitool的使用实例【转】

    本文转载自:http://blog.csdn.net/kangear/article/details/37961769 这个工具据说是基于BlueZ的,但是Android4.2以后不再采用BlueZ取 ...

  7. 批量梯度下降(Batch gradient descent) C++

    At each step the weight vector is moved in the direction of the greatest rate of decrease of the err ...

  8. Java语言中的程序流程控制

    (1. 流程控制 有三种基本技术可以改变程序的控制流程: A.调用方法 :将导致控制流程离开当前方法,转移到被调用的方法. B.选择  :   a. if / else 语句 b. switch语句 ...

  9. 第14章 Wi-Fi系统应用 14.1 了解Wi-Fi系统的结构

    Android平台中Wi-Fi系统从上到下主要包括Java框架类.Android适配器库.wpa_supplicant守护进程.驱动程序和协议,这几部分的系统结构如图14-3所示. (1)Wi-Fi用 ...

  10. (Go)05.基本数据类型和操作符

    保留的关键字段 1.Question1 package main import ( "fmt" ) func list(n int) { ; i <= n; i++ { fm ...