|
| 1 | + |
| 2 | +## Java 8中的常用函数式接口 |
| 3 | + |
| 4 | +| 函数式接口 | 函数描述符 | 原始类型特化 | |
| 5 | +|:-----:|:--------|:-------| |
| 6 | +| `Predicate<T>` | `T->boolean` | `IntPredicate,LongPredicate, DoublePredicate` | |
| 7 | +| `Consumer<T>` | `T->void` | `IntConsumer,LongConsumer, DoubleConsumer` | |
| 8 | +| `Function<T,R>` | `T->R` | `IntFunction<R>, IntToDoubleFunction, IntToLongFunction, LongFunction<R>, LongToDoubleFunction, LongToIntFunction, DoubleFunction<R>, ToIntFunction<T>, ToDoubleFunction<T>, ToLongFunction<T>` | |
| 9 | +| `Supplier<T>` | `()->T` | `BooleanSupplier,IntSupplier, LongSupplier, DoubleSupplier` | |
| 10 | +| `UnaryOperator<T>` | `T->T` | `IntUnaryOperator, LongUnaryOperator, DoubleUnaryOperator` | |
| 11 | +| `BinaryOperator<T>` | `(T,T)->T` | `IntBinaryOperator, LongBinaryOperator, DoubleBinaryOperator` | |
| 12 | +| `BiPredicate<L,R>` | `(L,R)->boolean` | | |
| 13 | +| `BiConsumer<T,U>` | `(T,U)->void` | `ObjIntConsumer<T>, ObjLongConsumer<T>, ObjDoubleConsumer<T>` | |
| 14 | +| `BiFunction<T,U,R>` | `(T,U)->R` | `ToIntBiFunction<T,U>, ToLongBiFunction<T,U>, ToDoubleBiFunction<T,U>` | |
| 15 | + |
| 16 | + |
| 17 | +## Lambdas及函数式接口的例子 |
| 18 | + |
| 19 | +| 使用案例 | Lambda 的例子 | 对应的函数式接口 | |
| 20 | +|:-----:|:--------|:-------| |
| 21 | +| 布尔表达式 | `(List<String> list) -> list.isEmpty()` | `Predicate<List<String>>` | |
| 22 | +| 创建对象 | `() -> new Project()` | `Supplier<Project>` | |
| 23 | +| 消费一个对象 | `(Project p) -> System.out.println(p.getStars())` | `Consumer<Project>` | |
| 24 | +| 从一个对象中选择/提取 | `(int a, int b) -> a * b` | `IntBinaryOperator` | |
| 25 | +| 比较两个对象 | `(Project p1, Project p2) -> p1.getStars().compareTo(p2.getStars())` | `Comparator<Project>或 BiFunction<Project, Project, Integer> 或 ToIntBiFunction<Project, Project>` | |
| 26 | + |
| 27 | + |
0 commit comments