andThen:default 方法。返回一个复合函数,该复合函数首先对其应用此函数它的输入,然后对结果应用 after 函数。如果任意一个函数的求值引发异常,则将其传递给组合函数的调用者。
identity:static 方法。返回一个始终返回其输入参数的函数。
3.1.3 方法举例
1)apply
测试代码:
public String upString(String str){ Function<String, String> function1 = s -> s.toUpperCase(); return function1.apply(str); } public static void main(String[] args) { System.out.println(upString("hello!")); }
通过 apply 调用具体的实现。执行结果:
2)compose
测试代码:
public static void main(String[] args) { Function<String, String> function1 = s -> s.toUpperCase(); Function<String, String> function2 = s -> "my name is "+s; String result = function1.compose(function2).apply("zhangSan"); System.out.println(result); }
执行结果
如结果所示:compose 先执行 function2 后执行 function1。
3)andThen
测试代码:
public static void main(String[] args) { Function<String, String> function1 = s -> s.toUpperCase(); Function<String, String> function2 = s -> "my name is "+s; String result = function1.andThen(function2).apply("zhangSan"); System.out.println(result); }
public class TestRun implements Runnable { @Override public void run() { System.out.println("TestRun is running!"); } } public static void main(String[] args) { Thread thread = new Thread(new TestRun()); thread.start(); }
执行结果:
如结果所示:当线程实行 start 方法时,执行 Runnable 的 run 方法中的内容。
4 Function 接口用法
Function 的主要用途是可以通过 lambda 表达式实现方法的内容。
4.1 差异处理
原代码:
@Data public class User { /** * 姓名 */ private String name; /** * 年龄 */ private int age; /** * 组员 */ private List<User> parters; } public static void main(String[] args) { User user =new User(); if(user ==null ||user.getAge() <18 ){ throw new RuntimeException("未成年!"); } }
执行结果:
使用 Function 接口后的代码:
@FunctionalInterface public interface testFunctionInfe { /** * 输入异常信息 * @param message */ void showExceptionMessage(String message); } public static testFunctionInfe doException(boolean flag){ return (message -> { if (flag){ throw new RuntimeException(message); } }); } public static void main(String[] args) { User user =new User(); doException(user ==null ||user.getAge() <18).showExceptionMessage("未成年!"); }
执行结果:
使用 function 接口前后都抛出了指定的异常信息。
4.2 处理 if…else…
原代码:
public static void main(String[] args) { User user =new User(); if(user==null){ System.out.println("新增用户"); }else { System.out.println("更新用户"); } }
使用 Function 接口后的代码:
public static void main(String[] args) { User user =new User(); Consumer trueConsumer = o -> { System.out.println("新增用户"); }; Consumer falseCnotallow= o -> { System.out.println("更新用户"); }; trueOrFalseMethdo(user).showExceptionMessage(trueConsumer,falseConsumer); } public static testFunctionInfe trueOrFalseMethdo(User user){ return ((trueConsumer, falseConsumer) -> { if(user==null){ trueConsumer.accept(user); }else { falseConsumer.accept(user); } }); } @FunctionalInterface public interface testFunctionInfe { /** * 不同分处理不同的事情 * @param trueConsumer * @param falseConsumer */ void showExceptionMessage(Consumer trueConsumer,Consumer falseConsumer); }
执行结果:
4.3 处理多个 if
原代码:
public static void main(String[] args) { String flag=""; if("A".equals(flag)){ System.out.println("我是A"); }else if ("B".equals(flag)) { System.out.println("我是B"); }else if ("C".equals(flag)) { System.out.println("我是C"); }else { System.out.println("没有对应的指令"); } }