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. fzu 2124

    #include<stdio.h> #include<queue> #include<math.h> #include<algorithm> #incl ...

  2. [转]C#——细说事务

    转自:风尘浪子 - 博客园 引言 其实事务在数据层.服务层.业务逻辑层多处地方都会使用到,在本篇文章将会为大家一一细说. 其中前面四节是事务的基础,后面的三节是事务的重点,对事务有基础的朋友可以跳过前 ...

  3. Codeforces Round #306 (Div. 2) D

    D. Regular Bridge time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  4. 2.3-STP生成树

    2.3-STP生成树     单点失效(signle point of failure)及其解决方法:     当两个Segement之间只有一条物理连接时就有可能出现单点失效→ 单方面的故障导致全网 ...

  5. bootstrap简单form表单样式-form-horizontal

    jsp代码: <div id="content" style="background-color: white;"> <form class= ...

  6. C#遍历DataSet与DataSet元素实现代码

    C#中的Dataset就像一个数据库,有多个表(Table),一般只有一个表,然后每个表中有行(DataRow)和列(DataColumn),DataRow[DataColumn]可以得到某行某列数据 ...

  7. c# 命令行下编译c#文件 // c# file类读写文件

    c# 命令行下编译c#文件 2010-03-01 15:02:14|  分类: c# 学习|字号 订阅     在 开始  ——>程序 ——>vstool中打开vs2008命令提示. 通过 ...

  8. C语言相关

    标准输入输出 格式化输入输出 int a,b; int arr[10]={1},*p=&b; double d; char ch,str[30]; scanf("%d%d" ...

  9. leetcode数学相关

    目录 166分数到小数 169/229求众数 238除自身以外数组的乘积 69Sqrt(x) 求平方根 231Power of Two 166分数到小数 给定两个整数,分别表示分数的分子 numera ...

  10. Python 36 死锁现象和递归锁、信号量、Event事件、线程queue

    一:死锁现象和递归锁 所谓死锁: 是指两个或两个以上的进程或线程在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产生了死锁,这些永远 ...