1. function
我们来看下function这个函数式接口里面都有什么方法
下面,我们来仔细研究下以下的方法
1.R apply(T t)
按照以往的逻辑,我们先来看下原始接口是怎么说的
翻译过来就是:将给定的参数应用到这个函数上,传入的参数类型为T返回类型为R
import java.util.function.Function;/** * @author Zerox * @date 2018/12/4 15:59 */public class TestFunction { public static void main(String[] args) { System.out.println(testFunction(2,i -> i * 2 + 1)); } public static int testFunction(int i, Function<Integer,Integer> function) { return function.apply(i); }}
运行结果:如下
2.compose(Function<? super V,? EXTENDS T> before)
按照以往的逻辑,我们先来看下原始接口是怎么说的
便宜美国vps
翻译过来就是:接收一个function类型的函数作为参数,这个函数真是越看越有意思。
import java.util.function.Function;/** * @author Zerox * @date 2018/12/4 16:44 */public class TestFunctionofcompose { public static void main(String[] args) { System.out.println(testFunction(2,i -> i * 2 + 1,j -> j * j)); } public static int testFunction(int i, Function<Integer,Integer> function1,Function<Integer,Integer> function2) { return function1.compose(function2).apply(i); }}
运行结果:如下
3.andThen(Function<? super R, ? extends V> after)
按照以往的逻辑,我们先来看下原始接口是怎么说的
翻译过来就是:就是现将传过来的参数执行apply(T t)方法,之后把apply(T t)里面返回的结果再去执行第二个Function函数
import java.util.function.Function;/** * @author Zerox * @date 2018/12/4 16:48 */public class TestFunctionofAndthen { public static void main(String[] args) { System.out.println(testFunction(2,i -> i * 2 + 1,j -> j * j)); } public static int testFunction(int i, Function<Integer,Integer> function1,Function<Integer,Integer> function2) { return function1.andThen(function2).apply(i); }}
运行结果:如下
4.andThen(Function<? super R, ? extends V> after)
按照以往的逻辑,我们先来看下原始接口是怎么说的
翻译过来就是:输入是什么。输出就是什么,暂时,我还没有什么遇到高级的用法
import java.util.function.Function;/** * @author Zerox * @date 2018/12/5 13:21 */public class TestFunctionIdentity { public static void main(String[] args) { Function<String,String> function = Function.identity(); String strValue = testIdentity(function); System.out.println(strValue); } public static String testIdentity(Function<String,String> function) { return function.apply(“hello world”); }}
运行结果:如下
*以后有机会继续去深入挖掘identity()方法,总感觉没有get到点上。
08131125