Java 中的 Function 接口案例讲解
目录
- 1. Function 的基本概念
- 1.1. 定义
- 1.2. 基本用法示例
- 2. Function 的方法详解
- 3. Function 的变体
- 3.1. 基本类型特化
- 3.2. 多参数变体
- 4. 实际应用
- 4.1. Stream API 中的 map 操作
- 4.2. 策略模式实现
- 4.3. 方法链式处理
- 4.4. 性能考虑
- 5. 高级使用
- 5.1. 柯里化(Currying)
- 5.1.1. 示例
- 5.1.2. 概念
- 5.1.3. 示例解析
- 5.1.4. 实际运用场景
- 5.2. 异常处理包装
- 5.2.1. 示例
- 5.2.2. 问题背景
- 5.2.3. 示例解析
- 5.2.4. 深入理解
- 5.2.5. 实际应用场景
- 5.2.6. 完整工具类示例
- 6. Java Consumer 接口详解
- 6.1. Consumer 的基本定义
- 6.2. 核心方法解析
- 6.3. Consumer 的变体
- 6.4. 典型运用场景
- 6.4.1. 集合遍历
- 6.4.2. 资源处理
- 6.4.3. 对象配置
- 6.4.4. 日志记录
- 6.5. 相关接口对比
- 6.6. 高级用法
- 6.6.1. 异常处理包装
- 6.6.2. 条件消费
- 6.6.3. 构建处理管道
- 6.7. 注意事项
- 6.7.1. 命名规范
- 6.7.2. 保持简洁
- 6.7.3. 避免副作用
Function 是 Java 8 引入的一个核心函数式接口,属于 java.util.function 包。它代表一个"接受一个参数并产生结果"的函数。
1. Function 的基本概念
1.1. 定义
T:输入类型(参数类型)
R:结果类型(返回类型)
唯一抽象方法:R apply(T t)
@FunctionalInterface public interface Function<T, R> { R apply(T t); // 还包含两个默认方法(后面会讲解) default <V> Function<V, R> compose(Function<? super V, ? extends T> before) default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) }
1.2. 基本用法示例
// 1. 定义一个将字符串转为长度的函数 Function<String, Integer> lengthFunction = s -> s.length(); System.out.println(lengthFunction.apply("Hello")); // 输出 5 // 2. 定义将数字转为字符串的函数 Function<Integer, String> stringify = num -> "Number: " + num; System.out.println(stringify.apply(42)); // 输出 "Number: 42"
2. Function 的方法详解
// 1. apply(T t) // 核心方法,执行函数转换: Function<String, String> upperCase = s -> s.toUpperCase(); String result = upperCase.apply("hello"); // 返回 "HELLO" ------------------------------------------------------------------------- // 2. compose(Function before) // 组合函数,先执行 before 函数,再执行当前函数: Function<Integer, String> intToStr = i -> "Value: " + i; Function<String, Integer> strToInt = s -> Integer.parseInt(s); // 先执行 strToInt,再执行 intToStr Function<String, String> composed = intToStr.compose(strToInt); System.out.println(composed.apply("123")); // 输出 "Value: 123" ------------------------------------------------------------------------- // 3. andThen(Function after) // 组合函数,先执行当前函数,再执行 after 函数: Function<String, Integer> parse = Integer::parseInt; Function<Integer, Integer> square = i -> i * i; // 先执行 parse,再执行 square Function<String, Integer> parseThenSquare = parse.andThen(square); System.out.println(parseThenSquare.apply("5")); // 输出 25 ------------------------------------------------------------------------- // 4. identity() // 静态方法,返回一个总是返回其输入参数的函数: Function<String, String> identity = Function.identity(); System.out.println(identity.apply("Same")); // 输出 "Same" -------------------------------------------------------------------------
3. Function 的变体
Java 还提供了 Function 的一些特化版本:
3.1. 基本类型特化
接口 方法签名 等价于
IntFunction<R> R apply(int value) Function<Integer, R>
DoubleFunction<R> R apply(double value) Function<Double, R>
LongFunction<R> R apply(long value) Function<Long, R>
IntFunction<String> intToString = i -> "Num: " + i; System.out.println(intToString.apply(10)); // 输出 "Num: 10"
3.2. 多参数变体
接口 方法签名
BiFunction<T,U,R> R apply(T t, U u)
BiFunction<String, String, String> concat = (s1, s2) -> s1 + s2; System.out.println(concat.apply("Hello", "World")); // 输出 HelloWorld
4. 实际应用
4.1. Stream API 中的 map 操作
List<String> names = Arrays.asList("Alice", "Bob", "Charlie"); // 将名字转为大写 List<String> upperNames = names.stream() .map(name -> name.toUpperCase()) // 这里使用Function .collect(Collectors.toList());
4.2. 策略模式实现
// 定义不同的处理策略 Map<String, Function<String, String>> processors = new HashMap<>(); processors.put("upper", String:编程客栈:toUpperCase); processors.put("lower", String::toLowerCase); processors.put("reverse", s -> new StringBuilder(s).reverse().toString()); // 使用策略 String input = "Hello"; String output = processors.get("upper").apply(input); // 输出 "HELLO"
4.3. 方法链式处理
Function<Integer, Integer> times2 = x -> x * 2; Function<Integer, Integer> minus1 = x -> x - 1; // 组合函数:先乘2再减1 Function<Integer, Integer> chain = times2.andThen(minus1); System.out.println(chaphpin.apply(10)); // 输出 19
4.4. 性能考虑
对象创建开销:每次使用 lambda 表达式都会创建一个新对象
自动装箱问题:对于基本类型,使用特化接口(如 IntFunction)更高效
JIT 优化:HotSpot 虚拟机会对 lambda 进行优化
5. 高级使用
5.1. 柯里化(Currying)
5.1.1. 示例
// 将二元函数转换为一元函数链 Function<Integer, Function<Integer, Integer>> adder = a -> b -> a + b; Function<Integer, Integer> add5 = adder.apply(5); System.out.println(add5.apply(3)); // 输出 8
5.1.2. 概念
柯里化的核心思想是:将一个接受多个参数的函数,转换成一系列使用一个参数的函数。
原始加法函数
BiFunction<Integer, Integer, Integer> normalAdd = (a, b) -> a + b;
柯里化后的加法函数
BiFunction<Integer, Integer, Integer> normalAdd = (a, b) -> a + b;
5.1.3. 示例解析
<code>Function<Integer, Function<Integer, Integer>> adder = a -> b -> a + b; Function<Integer, Integer> add5 = adder.apply(5); System.out.println(add5.apply(3)); // 输出 8 ------------------------------------------------------------------------ 1.定义柯里化函数: Function<Integer, Function<Integer, Integer>> adder = a -> b -> a + b; // 这是一个函数,接受一个整数a,返回另一个函数 // 返回的函数接受整数b,返回a + b的结果 ------------------------------------------------------------------------ 2.部分应用: Function<Integer, Integer> add5 = adder.apply(5); // 调用adder.apply(5)固定了第一个参数a=5 // 返回的新函数add5只需要一个参数b,计算5 + b ------------------------------------------------------------------------ 3.完成计算: add5.apply(3) // 返回8 // 向add5提供第二个参数3 // 执行计算5 + 3 = 8 ------------------------------------------------------------------------ </code>
5.1.4. 实际运用场景
5.1.4.1. 创建带前缀的日志函数
// 创建带前缀的日志函数 Function<String, Consumer<String>> logger = prefix -> message -> System.out.println("[" + prefix + "] " + message); Consumer<String> appLog = logger.apply("APP"); appLog.accept("Starting system..."); // 输出: [APP] Starting system...
5.1.4.2. Web开发中的中间件:
// 模拟中间件链 Function<String, Function<HttpRequest, HttpResponse>> middleware = authToken -> request -> { if(validToken(authToken)) { return process(request); } return new HttpResponse(403); };
5.2. 异常处理包装
5.2.1. 示例
@FunctionalInterface interface CheckedFunction<T, R> { R apply(T t) throws Exception; } public static <T, R> Function<T, R> wrap(CheckedFunction<T, R> checkedFunction) { return t -> { try { return checkedFunction.apply(t); } catch (Exception e) { throw new RuntimeException(e); } }; } // 使用 Function<String, Integer> safeParse = wrap(Integer::parseInt);
5.2.2. 问题背景
在Java中,函数式接口如Function不允许抛出受检异常,但像Integer.parseInt()这样的方法会抛出NumberFormatException(虽然是运行时异常,但其他方法如IO操作会抛出受检异常)。
5.2.3. 示例解析
1.@FunctionalInterface确保它只有一个抽象方法 @FunctionalInterface -------------------------------------------------------------------------------------- 2.这是一个自定义的函数式接口 与标准Function接口的关键区别是允许apply()抛出Exception interface CheckedFunction<T, R> { R apply(T t) throws Exception; } -------------------------------------------------------------------------------------- 3.将CheckedFunction转换为标准的Function 使用try-catch捕获所有异常 将受检异常包装为RuntimeException public static <T, R> Function<T, R> wrap(CheckedFunction<T, R> checkedFunction) { return t -> { // 返回一个标准的Function try { return checkedFunction.apply(t); // 执行可能抛出异常的操作 } catch (Exception e) { throw new RuntimeException(e); // 包装为运行时异常 } }; } -------------------------------------------------------------------------------------- 4.Integer::parseInt方法引用作为CheckedFunction输入 返回的safeParse是一个标准的Function,可以在Stream等场景直接使用 Function<String, Integer> safeParse = wrap(Integer::parseInt);
5.2.4. 深入理解
5.2.4.1. 为什么需要这样包装?
Java的函数式编程接口(如Function、Consumer等)的抽象方法不允许抛出受检异常。这种包装模式解决了:
允许在lambda表达式中使用会抛出异常的方法
保持与现有函数式接口的兼容性
5.2.4.2. 统一异常处理方式
定制异常处理:
public static <T, R> Function<T, R> wrap( CheckedFunction<T, R> checkedFunction, Function<Exception, R> exceptionHandler) { return t -> { try { return checkedFunction.apply(t); } catch (Exception e) { return exceptionHandler.apply(e); } }; } // 使用:解析失败时返回null Function<String, Integer> safeParse = wrap( Integer::parseInt, e -> null );
5.2.5. 实际应用场景
5.2.5.1. Stream处理中的异常
List<String> numbers = Arrays.asList("1", "2", "abc", "4"); // 不使用包装会很难处理异常 List<Integer> parsed = numbers.stream() .map(wrap(Integer::parseInt)) // 使用包装方法 .collect(Collectors.toList()); // 遇到错误会抛出RuntimeException
5.2.5.2. 文件操作
// 读取文件所有行 Function<Path, List<String>> readLines = wrap(path -> Files.readAllLines(path, StandardCharsets.UTF_8)); List<String> lines = readLines.apply(Paths.get("data.txt"));
5.2.5.3. 数据库操作
// 查询用户 Function<Long, User> findUser = wrap(id -> userRepository.findById(id).orElseThrow()); User user = findUser.apply(123L);
5.2.6. 完整工具类示例
public class FunctionWrappers { @FunctionalInterface public interface CheckedFunction<T, R> { R apply(T t) throws Exception; } // 基本包装方法 public static <T, R> Function<T, R> wrap(CheckedFunction<T, R> checphpkedFunction) { return t -> { try { return checkedFunction.apply(t); } catch (Exception e) { throw new RuntimeException(e); } }; } // 带异常处理的包装 public static <T, R> Function<T, R> wrap( CheckedFunction<T, R> checkedFunction, Function<Exception, R> exceptionHandler) { return t -> { try { return checkedFunction.apply(t); } catch (Exception e) { return exceptionHandler.apply(e); } }; } // 针对Consumer的包装 @FunctionalInterface public interface CheckedConsumer<T> { void accept(T t) throws Exception; } public static <T> Consumer<T> wrapConsumer(CheckedConsumer<T> checkedConsumer) { return t ->js; { try { checkedConsumer.accept(t); } catch (Exception e) { throw new RuntimeException(e); } }; } }
6. Java Consumer 接口详解
Consumer 是 Java 8 引入的一个核心函数式接口,属于 java.util.function 包。它代表一个"接受单个输入参数但不返回结果"的操作。
6.1. Consumer 的基本定义
T:输入参数类型
核心方法:accept(T t) 执行操作
特点:有输入无输出(与 Function 不同)
@FunctionalInterface public interface Consumer<T> { void accept(T t); // 核心抽象方法 // 默认方法(组合操作) default Consumer<T> andThen(Consumer<? super T> after) }
6.2. 核心方法解析
1. accept(T t) 执行消费操作: Consumer<String> printConsumer = s -> System.out.println(s); printConsumer.accept("Hello"); // 输出 "Hello" ---------------------------------------------------------------------------- 2. andThen(Consumer after) 组合多个 Consumer(按顺序执行): Consumer<String> printUpper = s -> System.out.println(s.toUpperCase()); Consumer<String> printLower = s -> System.out.println(s.toLowerCase()); Consumer<String> combined = printUpper.andThen(printLower); combined.accept("Java"); // 输出: // JAVA // java ----------------------------------------------------------------------------
6.3. Consumer 的变体
Java 还提供了 Consumer 的特化版本:
接口 方法签名 说明
IntConsumer void accept(int) 处理 int 类型
LongConsumer void accept(long) 处理 long 类型
DoubleConsumer void accept(double) 处理 double 类型
BiConsumer<T,U> void accept(T t, U u) 处理两个参数
6.4. 典型运用场景
6.4.1. 集合遍历
List<String> names = List.of("Alice", "Bob", "Charlie"); // 传统方式 for (String name : names) { System.out.println(name); } // 使用 Consumer names.forEach(name -> System.out.println(name)); // 使用方法引用 names.forEach(System.out::println);
6.4.2. 资源处理
Consumer<Path> fileProcessor = path -> { try { String content = Files.readString(path); System.out.println(content); } catch (IOException e) { throw new RuntimeException(e); } }; fileProcessor.accept(Paths.get("data.txt"));
6.4.3. 对象配置
class Person { String name; int age; // setters... } Consumer<Person> configurator = p -> { p.setName("Default"); p.setAge(30); }; Person person = new Person(); configurator.accept(person);
6.4.4. 日志记录
BiConsumer<String, Object> logger = (level, msg) -> { System.out.printf("[%s] %s - %s%n", LocalDateTime.now(), level, msg); }; logger.accept("INFO", "Application started");
6.5. 相关接口对比
接口 方法签名 特点
Consumer<T> void accept(T) 有输入无输出
Function<T,R> R apply(T) 有输入有输出
Supplier<T> T get() 无输入有输出
Predicate<T> boolean test(T) 有输入,输出布尔值
6.6. 高级用法
6.6.1. 异常处理包装
@FunctionalInterface interface CheckedConsumer<T> { void accept(T t) throws Exception; } public static <T> Consumer<T> wrap(CheckedConsumer<T> checkedConsumer) { return t -> { try { checkedConsumer.accept(t); js } catch (Exception e) { throw new RuntimeException(e); } }; } // 使用 Consumer<Path> safeFileReader = wrap(path -> { String content = Files.readString(path); System.out.println(content); });
6.6.2. 条件消费
public static <T> Consumer<T> conditional( Predicate<T> condition, Consumer<T> consumer) { return t -> { if (condition.test(t)) { consumer.accept(t); } }; } // 只打印长度大于3的字符串 Consumer<String> selectivePrint = conditional( s -> s.length() > 3, System.out::println ); selectivePrint.accept("Hi"); // 不输出 selectivePrint.accept("Hello"); // 输出 "Hello"
6.6.3. 构建处理管道
Consumer<String> pipeline = ((Consumer<String>) s -> System.out.println("Original: " + s)) .andThen(s -> System.out.println("Upper: " + s.toUpperCase())) .andThen(s -> System.out.println("Length: " + s.length())); pipeline.accept("Java"); /* 输出: Original: Java Upper: JAVA Length: 4 */
6.7. 注意事项
6.7.1. 命名规范
// 好的命名 Consumer<String> logMessage = msg -> System.out.println(msg); // 不好的命名 Consumer<String> c = m -> System.out.println(m);
6.7.2. 保持简洁
// 简单逻辑直接用lambda names.forEach(System.out::println); // 复杂逻辑提取方法 names.forEach(this::processName);
6.7.3. 避免副作用
// 不推荐(修改外部状态) List<String> result = new ArrayList<>(); names.forEach(name -> result.add(name.toUpperCase())); // 推荐(使用stream) List<String> result = names.stream() .map(String::toUpperCase) .collect(Collectors.toList());
到此这篇关于Java 中的 Function 接口的文章就介绍到这了,更多相关Java Function 接口内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
精彩评论