本文目录导读:

我来详细介绍Java函数式接口的识别方法和实际案例。
函数式接口的核心特征
函数式接口必须满足以下条件:
- 有且只有一个抽象方法
- 可以有多个默认方法或静态方法
- 使用
@FunctionalInterface注解(可选,但推荐)
识别方法
通过注解识别
@FunctionalInterface // 编译时检查
public interface Runnable {
void run(); // 唯一抽象方法
}
通过方法数量识别
// 是函数式接口:只有一个抽象方法
public interface MyFunction<T, R> {
R apply(T t); // 唯一抽象方法
default void log() { } // 默认方法不影响
static void info() { } // 静态方法不影响
}
// 不是函数式接口:两个抽象方法
public interface NotFunctional {
void method1();
void method2();
}
实际案例演示
案例1:简单的条件过滤
import java.util.*;
import java.util.function.Predicate;
public class FunctionalInterfaceDemo {
public static void main(String[] args) {
// 识别:Predicate<T> 是函数式接口
// 抽象方法:boolean test(T t)
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
// 使用Lambda表达式实现函数式接口
Predicate<Integer> isEven = n -> n % 2 == 0;
Predicate<Integer> isPositive = n -> n > 0;
// 组合使用
List<Integer> result = filter(numbers, isEven.and(isPositive));
System.out.println("偶数且正数: " + result);
}
public static List<Integer> filter(List<Integer> list, Predicate<Integer> predicate) {
List<Integer> result = new ArrayList<>();
for (Integer num : list) {
if (predicate.test(num)) {
result.add(num);
}
}
return result;
}
}
案例2:自定义函数式接口
// 定义函数式接口
@FunctionalInterface
interface Calculator {
int calculate(int a, int b);
// 默认方法
default void printResult(int a, int b) {
System.out.println("Result: " + calculate(a, b));
}
// 静态方法
static Calculator getAddition() {
return (a, b) -> a + b;
}
}
public class CustomFunctionalInterfaceDemo {
public static void main(String[] args) {
// 使用Lambda表达式实现
Calculator addition = (a, b) -> a + b;
Calculator subtraction = (a, b) -> a - b;
Calculator multiplication = (a, b) -> a * b;
// 识别:接口只有calculate一个抽象方法
System.out.println("10 + 5 = " + addition.calculate(10, 5));
System.out.println("10 - 5 = " + subtraction.calculate(10, 5));
// 使用默认方法
multiplication.printResult(10, 5);
// 使用静态方法
Calculator add = Calculator.getAddition();
System.out.println("Static: 10 + 5 = " + add.calculate(10, 5));
}
}
案例3:实际业务场景
import java.util.function.*;
public class BusinessCase {
public static void main(String[] args) {
// 识别多种函数式接口
processUsers();
}
static void processUsers() {
List<User> users = Arrays.asList(
new User("Alice", 28, "Beijing"),
new User("Bob", 22, "Shanghai"),
new User("Charlie", 35, "Beijing")
);
// Consumer<T>:消费型接口
// 抽象方法:void accept(T t)
Consumer<User> printUser = u -> System.out.println(u.name);
// Function<T, R>:转换型接口
// 抽象方法:R apply(T t)
Function<User, String> getName = u -> u.name;
// Supplier<T>:供给型接口
// 抽象方法:T get()
Supplier<User> defaultUser = () -> new User("Default", 0, "Unknown");
// 组合使用
users.forEach(printUser);
List<String> names = users.stream()
.map(getName)
.collect(Collectors.toList());
System.out.println("Names: " + names);
}
}
class User {
String name;
int age;
String city;
User(String name, int age, String city) {
this.name = name;
this.age = age;
this.city = city;
}
}
Java内置的常用函数式接口
| 接口 | 抽象方法 | 用途 |
|---|---|---|
Predicate<T> |
boolean test(T t) |
条件判断 |
Consumer<T> |
void accept(T t) |
消费数据 |
Function<T,R> |
R apply(T t) |
类型转换 |
Supplier<T> |
T get() |
生产数据 |
UnaryOperator<T> |
T apply(T t) |
单参数同类型操作 |
BinaryOperator<T> |
T apply(T t1, T t2) |
双参数同类型操作 |
- 看注解:查找
@FunctionalInterface注解 - 数方法:确认只有1个抽象方法
- 排除默认/静态:这些不影响函数式接口的属性
- 继承关系:如果继承的接口有抽象方法,也要计入
这样就能准确识别和使用Java函数式接口了。