函数式编程
在 Java 中,可以通过使用函数式接口(Functional Interface)和Lambda 表达式来将一个可执行的函数 作为 方法参数传递。
Java 8 引入了 Lambda 表达式和函数式接口,使得这一过程变得更加简洁和直观。
函数式接口
函数式接口是指仅包含一个抽象方法的接口。
Java 提供了一些内置的函数式接口,如 Function<T, R>、Consumer<T>、Supplier<T>、Predicate<T> 等等,也可以自定义自己的函数式接口。
内置的函数式接口
Function<T, R>
Function<T, R> 是一个常见的函数式接口,它表示一个接受一个输入并返回一个结果的函数。
import java.util.function.Function;
public class FunctionExample {
// 定义一个方法,接收一个 Function 作为参数
public static <T, R> R applyFunction(T input, Function<T, R> function) {
return function.apply(input);
}
public static void main(String[] args) {
// 使用 Lambda 表达式作为参数
String result = applyFunction("Hello", s -> s.toUpperCase());
System.out.println(result); // 输出: HELLO
// 使用方法引用作为参数
Integer length = applyFunction("Hello", String::length);
System.out.println(length); // 输出: 5
}
}
Consumer<T>
Consumer<T> 是一个函数式接口,它表示一个接受单个输入参数但不返回结果的操作。
import java.util.function.Consumer;
public class ConsumerExample {
// 定义一个方法,接收一个 Consumer 作为参数
public static <T> void executeConsumer(T input, Consumer<T> consumer) {
consumer.accept(input);
}
public static void main(String[] args) {
// 使用 Lambda 表达式作为参数
executeConsumer("Hello", s -> System.out.println(s));
// 使用方法引用作为参数
executeConsumer(123, System.out::println);
}
}
Supplier<T>
Supplier<T> 是一个函数式接口,它表示一个不接受任何参数但返回一个结果的供应商。
import java.util.function.Supplier;
public class SupplierExample {
// 定义一个方法,接收一个 Supplier 作为参数
public static <T> T getSuppliedValue(Supplier<T> supplier) {
return supplier.get();
}
public static void main(String[] args) {
// 使用 Lambda 表达式作为参数
String value = getSuppliedValue(() -> "Hello, World!");
System.out.println(value); // 输出: Hello, World!
// 使用方法引用作为参数
Integer randomValue = getSuppliedValue(() -> (int) (Math.random() * 100));
System.out.println(randomValue);
}
}
Predicate<T>
Predicate<T> 是一个函数式接口,它表示一个接受单个输入参数并返回布尔值的谓词。
import java.util.function.Predicate;
public class PredicateExample {
// 定义一个方法,接收一个 Predicate 作为参数
public static <T> boolean checkCondition(T input, Predicate<T> predicate) {
return predicate.test(input);
}
public static void main(String[] args) {
// 使用 Lambda 表达式作为参数
boolean isLongString = checkCondition("Hello, World!", s -> s.length() > 5);
System.out.println(isLongString); // 输出: true
// 使用方法引用作为参数
boolean isPositive = checkCondition(-10, Math::signum);
System.out.println(isPositive); // 输出: false
}
}
自定义函数式接口
@FunctionalInterface
interface TriFunction<T, U, V, R> {
R apply(T t, U u, V v);
}
public class CustomFunctionExample {
// 定义一个方法,接收一个 TriFunction 作为参数
public static <T, U, V, R> R applyTriFunction(T t, U u, V v, TriFunction<T, U, V, R> triFunction) {
return triFunction.apply(t, u, v);
}
public static void main(String[] args) {
// 使用 Lambda 表达式作为参数
int sum = applyTriFunction(1, 2, 3, (a, b, c) -> a + b + c);
System.out.println(sum); // 输出: 6
// 使用方法引用作为参数
String concatenated = applyTriFunction("Hello", " ", "World", String::concat);
System.out.println(concatenated); // 输出: Hello World
}
}
在这个例子中,我们定义了一个名为 TriFunction 的自定义函数式接口,它接受三个输入参数并返回一个结果。然后我们在 applyTriFunction 方法中使用这个接口。
浙公网安备 33010602011771号