











java.util.function 是 Java 8 专门为 Lambda / Stream 提供的函数式接口包,一共只有 4 大类核心接口,剩下全是它们的变形。
T get()Supplier<String> supplier = () -> "hello";
String s = supplier.get();
void accept(T t)Consumer<String> consumer = System.out::println;
consumer.accept("test");
R apply(T t)Function<String, Integer> fun = String::length;
int len = fun.apply("abc");
boolean test(T t)Predicate<String> pred = s -> s.length() > 3;
boolean b = pred.test("abcd");
void accept(T, U)R apply(T, U)boolean test(T, U)IntFunction<R>、LongFunction<R>、DoubleFunction<R>ToIntFunction<T>、ToLongFunction<T>IntConsumer、LongConsumer、IntPredicate 等T apply(T t)(一元运算)T apply(T t, T u)(二元运算,如求和)| 接口 | Stream 方法 | 作用 |
|---|---|---|
| Predicate | filter() | 过滤 |
| Function<T, R> | map() | 映射转换 |
| Consumer | forEach()/peek() | 遍历消费 |
| Supplier | generate() | 生成流数据 |
| BinaryOperator | reduce() | 聚合计算 |
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。