Java 函数式接口
1 Supplier<T>
供应商
用途:提供/生成数据,无参数,返回一个值
抽象方法:T get()
Supplier<String> randomString = () -> "Hello World";
Supplier<Integer> randomNumber = () -> (int)(Math.random() * 100);
System.out.println(randomString.get()); // 输出: Hello World
System.out.println(randomNumber.get()); // 输出: 随机数
2 Function<T,R>
函数
用途:接收一个参数,返回一个结果(转换操作)
抽象方法:R apply(T t)
Function<String, Integer> stringToInt = Integer::parseInt;
Function<Integer, String> intToString = Object::toString;
Integer num = stringToInt.apply("123"); // 123
String str = intToString.apply(456); // "456"
3 Consumer<T>
消费者
用途:接收一个参数,执行操作,不返回结果
抽象方法:void accept(T t)
Consumer<String> printer = System.out::println;
Consumer<List<String>> clearer = List::clear;
printer.accept("Hello"); // 输出: Hello
List<String> list = new ArrayList<>(Arrays.asList("a", "b"));
clearer.accept(list); // 清空列表
4 Predicate<T>
断言
用途:接收一个参数,返回boolean值(条件判断)
抽象方法:boolean test(T t)
Predicate<String> isLong = s -> s.length() > 5;
Predicate<Integer> isEven = n -> n % 2 == 0;
System.out.println(isLong.test("Hello")); // false
System.out.println(isEven.test(10)); // true
5 BiFunction<T,U,R>
二元函数
用途:接收两个不同类型的参数,返回一个结果
BiFunction<Integer, Integer, Integer> adder = (a, b) -> a + b;
BiFunction<String, String, String> concat = (s1, s2) -> s1 + s2;
BiFunction<String, Integer, String> repeat = (s, n) -> s.repeat(n);
int sum = adder.apply(3, 5); // 8
String combined = concat.apply("Hello", "World"); // "HelloWorld"
String repeated = repeat.apply("Hi", 3); // "HiHiHi"
类名引用成员方法ClassName::instanceMethod
:这种语法用于引用特定类的实例方法,但需要第一个参数作为方法调用的接收者
6 UnaryOperator<T>
一元操作符
UnaryOperator<Integer> square = x -> x * x;
UnaryOperator<String> trim = String::trim;
UnaryOperator<List<String>> sorter = list -> {
list.sort(String::compareTo);
return list;
};
int squared = square.apply(5); // 25
String trimmed = trim.apply(" hello "); // "hello"
7 BinaryOperator<T>
二元操作符
BinaryOperator<Integer> multiplier = (a, b) -> a * b;
BinaryOperator<String> joiner = (s1, s2) -> s1 + "-" + s2;
BinaryOperator<Double> max = Math::max;
int product = multiplier.apply(4, 5); // 20
String joined = joiner.apply("A", "B"); // "A-B"
double maximum = max.apply(3.5, 2.1); // 3.5
实际应用场景
场景1:延迟执行(Supplier)
// 只有在需要时才执行昂贵操作
Supplier<ExpensiveObject> lazyLoader = () -> createExpensiveObject();
// ... 其他代码
if (needed) {
ExpensiveObject obj = lazyLoader.get(); // 现在才创建
}
场景2:策略模式(Function)
Map<String, Function<String, String>> processors = new HashMap<>();
processors.put("upper", String::toUpperCase);
processors.put("lower", String::toLowerCase);
String input = "Hello";
String result = processors.get("upper").apply(input); // "HELLO"
场景3:条件过滤(Predicate)
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> evens = numbers.stream()
.filter(n -> n % 2 == 0) // Predicate
.collect(Collectors.toList()); // [2, 4]
场景4:事件处理(Consumer)
List<Consumer<String>> listeners = new ArrayList<>();
listeners.add(System.out::println);
listeners.add(message -> logToFile(message));
// 通知所有监听器
listeners.forEach(listener -> listener.accept("Event occurred"));
分类:
Java-Backend
标签:
Java
版权申明
本文系作者 @xiin 原创发布在To Future$站点。未经许可,禁止转载。
暂无评论数据