JavaFunction的使用

一、基础概念与核心方法

​定义与作用​

Function<T, R> 是一个函数式接口,接收类型为 T 的输入参数,返回类型为 R 的结果。其核心方法为 apply(T t)。例如,将字符串转换为整数长度:

java

Function<String, Integer> lengthFunction = s -> s.length();

System.out.println(lengthFunction.apply("Java")); // 输出:4

​链式组合方法​

andThen() :先执行当前函数,再将结果传递给后续函数。

compose() :先执行参数中的函数,再将结果传递给当前函数。

示例:数学运算组合:

java

Function<Integer, Integer> multiplyByTwo = x -> x * 2;

Function<Integer, Integer> addThree = x -> x + 3;

// 先乘2再加3

Function<Integer, Integer> combined1 = multiplyByTwo.andThen(addThree);

// 先加3再乘2

Function<Integer, Integer> combined2 = addThree.compose(multiplyByTwo);

System.out.println(combined1.apply(5)); // 输出:13

System.out.println(combined2.apply(5)); // 输出:16

​异常处理​

使用 orElseThrow() 处理可能的空值或异常情况:

java

Function<String, Integer> parseFunction = s -> Integer.parseInt(s);

String input = "123";

int result = parseFunction.apply(input).orElseThrow(() -> new IllegalArgumentException("无效输入"));

二、实际应用场景

​数据转换​

在集合操作中,Function 可与 Stream API 结合,简化数据转换逻辑。例如,将字符串列表转为整数列表:

java

List numbers = Arrays.asList("1", "2", "3");

List intList = numbers.stream()

.map(Integer::parseInt)

.collect(Collectors.toList());

​业务逻辑封装​

替代冗长的条件分支,例如根据商品类型计算折扣价:

java

Function<Product, BigDecimal> discountFunction = product -> {

switch (product.getType()) {

case "A": return product.getPrice().multiply(new BigDecimal("0.9"));

case "B": return product.getPrice().multiply(new BigDecimal("0.8"));

default: return product.getPrice();

}

};

BigDecimal finalPrice = discountFunction.apply(product);

​动态组合操作​

在数据处理流水线中,灵活组合多个函数:

java

Function<String, String> trim = String::trim;

Function<String, String> toUpper = String::toUpperCase;

Function<String, String> addPrefix = s -> "Result: " + s;

String output = trim.andThen(toUpper).andThen(addPrefix).apply(" hello ");

System.out.println(output); // 输出:Result: HELLO

三、与 JDK 新特性的结合

​模式匹配(Java 17+)​​

在 switch 表达式中结合 Function,简化类型判断逻辑:

java

Function<Object, String> formatter = obj -> switch (obj) {

case Integer i -> "Integer: " + i;

case String s -> "String: " + s;

default -> "Unknown";

};

System.out.println(formatter.apply(10)); // 输出:Integer: 10

​记录类(Record)​​

通过 Function 转换记录类对象,例如提取字段:

java

record Person(String name, int age) {}

Function<Person, String> getName = Person::name;

System.out.println(getName.apply(new Person("Alice", 30))); // 输出:Alice

四、最佳实践建议

​避免过度嵌套:链式调用过长时,可拆分为多个中间函数。

​优先使用方法引用:如 String::length 替代 s -> s.length(),提升可读性。

​结合 Optional:处理可能为空的输入,例如 Function.andThen(Optional::ofNullable)。

通过上述方法,Function 能有效减少冗余代码(如消除 80% 的 if-else 分支),提升代码的模块化和可维护性。实践中可根据具体场景选择组合方式,并探索与 Lambda、Stream 等特性的深度结合。

相关推荐
逍遥德3 分钟前
MQTT教程详解-05.SpringBoot集成mqtt client 性能分析
java·spring boot·spring·mt
云烟成雨TD8 分钟前
Spring AI 1.x 系列【54】Retry 机制分析
java·人工智能·spring
weixin_5231853210 分钟前
Collections.unmodifiableMap详解:真的不可修改吗?
java·linux·前端
点燃大海12 分钟前
SpringAI构建智能体
java·spring boot·spring·springai智能体
xier_ran13 分钟前
【infra之路】02_RadixAttention与KV_Cache管理
java·spring boot·spring
黑马师兄27 分钟前
RAG混合检索深度解析:让AI真正找到你要的内容
java·人工智能·ai·agent·rag·ai-native
码客日记31 分钟前
Spring Boot 配置文件敏感信息加密(Jasypt 企业级完整方案)
java·spring boot·git
宋拾壹1 小时前
同时添加多个类目
android·开发语言·javascript
凡人叶枫1 小时前
Effective C++ 条款04:确定对象被使用前已先被初始化
java·linux·开发语言·c++·嵌入式开发
极客先躯1 小时前
高级java每日一道面试题-2026年02月01日-实战篇[Docker]-Docker Volume 的生命周期管理是怎样的?
java·运维·docker·容器·持久化·架构图·容器卷