初识函数式接口

72 阅读2分钟

1. 概述

函数式接口(Functional Interface) 是 Java 8 引入的核心概念之一,它是支持 Lambda 表达式和方法引用的基础。函数式接口为 Java 提供了更灵活的编程能力,使得代码更简洁、更具表达力。

2. 定义

  • 核心特征:仅包含一个抽象方法(Single Abstract Method, SAM)的接口。
  • 注解标记:可通过 @FunctionalInterface 显式声明,编译器会强制检查接口是否符合规范。
  • 默认/静态方法:允许包含多个默认方法或静态方法,不影响其函数式接口特性。
@FunctionalInterface
public interface BranchHandleFunction {

	/**
     * 唯一抽象方法
     * @param runnable
     * @param runnable2
     */
	void handler(Runnable runnable, Runnable runnable2);

	/**
     * 默认方法1
     */
	default void method() {}

	/**
     * 默认方法2
     */
	default void method2() {}

	/**
     * 静态方法1
     */
	static void method3() {}

	/**
     * 静态方法2
     */
	static void method4() {}

}

3. 内置函数式接口

Java 8 在 java.util.function 包中提供了四大核心函数式接口:

3.1. Consumer<T>

  • 功能:消费数据,无返回值
  • 方法void accept(T t)
  • 变种BiConsumer<T,U>, IntConsumer
Consumer<String> printer = s -> System.out.println(s);
printer.accept("Hello Functional!");

3.2. Supplier<T>

  • 功能:提供数据,无输入参数
  • 方法T get()
Supplier<Double> randomSupplier = Math::random;
System.out.println(randomSupplier.get());

3.3. Function<T,R>

  • 功能:类型转换,接收 T 返回 R
  • 方法R apply(T t)
  • 变种UnaryOperator<T>, BiFunction<T,U,R>
Function<String, Integer> parser = Integer::parseInt;
System.out.println(parser.apply("123") + 100);

3.4. Predicate<T>

  • 功能:条件判断,返回 boolean
  • 方法boolean test(T t)
  • 组合方法and(), or(), negate()
Predicate<String> isLong = s -> s.length() > 5;
System.out.println(isLong.test("Hello")); // false

4. 自定义函数式接口

@FunctionalInterface
public interface CalculateFunction<N, R> {
    R calculate(N n);
}

CalculateFunction<BigDecimal, BigDecimal> fen = (money) -> new BigDecimal(100).multiply(money);

5. 高级应用

5.1. 方法引用

  • 静态方法引用ClassName::staticMethod
  • 实例方法引用instance::method
  • 构造函数引用ClassName::new
Function<String, LocalDate> dateParser = LocalDate::parse;

5.2. 组合操作

通过 andThen()compose() 实现函数管道:

Function<Integer, Integer> square = x -> x * x;
Function<Integer, String> converter = x -> "Value: " + x;

square.andThen(converter).apply(5); // "Value: 25"

5.3. 柯里化(Currying)

将多参数函数分解为多个单参数函数:

Function<Integer, Function<Integer, Integer>> adder = 
    a -> b -> a + b;
    
adder.apply(5).apply(3); // 8