Java 常用函数式接口之Function接口
JDK提供了大量常用的函数式接口以丰富Lambda的典型使用场景,它们主要在 java.util.function 包中被提供。 下面是最简单的Function接口及使用示例。
Function接口概述
java.util.function.Function<T,R> 接口用来根据一个类型的数据得到另一个类型的数据,前者称为前置条件,后者称为后置条件。
复制@FunctionalInterface public interface Function<T, R> { R apply(T t); ...... }
抽象方法:apply
Function 接口中最主要的抽象方法为: R apply(T t) ,根据类型T的参数获取类型R的结果。 使用的场景例如:将 String 类型转换为 Integer 类型。
复制import java.util.function.Function; public class DemoFunctionApply { public static void main(String[] args) { method(s -> Integer.parseInt(s)); } private static void method(Function<String, Integer> function) { int num = function.apply("10"); System.out.println(num + 20); } }
运行程序,控制台输出:
复制30
当然,最好是通过方法引用的写法。
默认方法:andThen
Function 接口中有一个默认的 andThen 方法,用来进行组合操作。JDK源代码如:
复制default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) { Objects.requireNonNull(after); return (T t) -> after.apply(apply(t)); }
该方法同样用于“先做什么,再做什么”的场景,和 Consumer 中的 andThen 差不多:
复制import java.util.function.Function; public class DemoFunctionAndThen { public static void main(String[] args) { method( str -> Integer.parseInt(str)+10, i -> i *= 10 ); } private static void method(Function<String, Integer> one, Function<Integer, Integer> two) { int num = one.andThen(two).apply("10"); System.out.println(num + 20); } }
运行程序,控制台输出:
复制220
第一个操作是将字符串解析成为int数字,第二个操作是乘以10。两个操作通过 andThen 按照前后顺序组合到了一 起。
复制请注意,Function的前置条件泛型和后置条件泛型可以相同。
练习:自定义函数模型拼接
题目
请使用 Function 进行函数模型的拼接,按照顺序需要执行的多个函数操作为:
复制String str = "赵丽颖,20";
- 将字符串截取数字年龄部分,得到字符串;
- 将上一步的字符串转换成为int类型的数字;
- 将上一步的int数字累加100,得到结果int数字。
解答
复制import java.util.function.Function; public class DemoFunction { public static void main(String[] args) { String str = "赵丽颖,20"; int age = getAgeNum( str, s -> s.split(",")[1], s -> Integer.parseInt(s), n -> n += 100 ); System.out.println(age); } private static int getAgeNum(String str, Function<String, String> one, Function<String, Integer> two, Function<Integer, Integer> three) { return one.andThen(two).andThen(three).apply(str); } }
运行程序,控制台输出:
复制120
本文来自博客园,作者:LeeHua,转载请注明原文链接:https://www.cnblogs.com/liyihua/p/12286100.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)