Skip to content

jdk8特性

一、lambda表达式

java
Thread thread = new Thread(() -> System.out.println("aaa"));

二、jdk8新接口

| 函数式接口 | 函数描述符 | 原始类型特化 |

| ----------------- | -------------- | ------------------------------------------------------------ |

| Predicate<T> | T->boolean | IntPredicate, LongPredicate, DoublePredicate |

| Consumer<T> | T->void | IntConsumer, LongConsumer, DoubleConsumer |

| Function<T,R> | T->R | IntFunction<R>, IntToDoubleFunction, IntToLongFunction, LongFunction<R>, LongToDoubleFunction, LongToIntFunction, DoubleFunction<R>, ToIntFunction<T>, ToDoubleFunction<T>, ToLongFunction<T> |

| Supplier<T> | ()->T | BooleanSupplier, IntSupplier, LongSupplier, DoubleSupplier |

| UnaryOperator<T> | T->T | IntUnaryOperator, LongUnaryOperator, DoubleUnaryOperator |

| BinaryOperator<T> | (T,T)->T | IntBinaryOperator, LongBinaryOperator, DoubleBinaryOperator |

| BiPredicate\<L,R\> | (L,R)-&gt;boolean | |

| BiConsumer&lt;T,U&gt; | (T,U)-&gt;void | ObjIntConsumer&lt;T&gt;, ObjLongConsumer&lt;T&gt;, ObjDoubleConsumer&lt;T&gt; |

| BiFunction&lt;T,U,R&gt; | (T,U)-&gt;R | ToIntBiFunction&lt;T,U&gt;, ToLongBiFunction&lt;T,U&gt;, ToDoubleBiFunction&lt;T,U&gt; |

三、使用案例

| 使用案例 | Lambda的例子 | 对应的函数式接口 |

| --------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |

| 布尔表达式 | (List&lt;String&gt; list) -&gt; list.isEmpty() | Predicate&lt;List&lt;String&gt;&gt; |

| 创建对象 | () -&gt; new Apple(10) | Supplier&lt;Apple&gt; |

| 消费一个对象 | (Apple a) -&gt; System.out.println(a.getWeight()) | Consumer&lt;Apple&gt; |

| 从一个对象中选择/提取 | (String s) -&gt; s.length() | Function&lt;String, Integer&gt; 或 ToIntFunction&lt;String&gt; |

| 合并两个值 | (int a, int b) -&gt; a * b | IntBinaryOperator |

| 比较两个对象 | (Apple a1, Apple a2) -&gt; a1.getWeight().compareTo(a2.getWeight()) | Comparator&lt;Apple&gt; 或 BiFunction&lt;Apple, Apple, Integer&gt; 或 ToIntBiFunction&lt;Apple, Apple&gt; |

四、方法引用

Lambda | 等效的方法引用

:---: | :---:

(Apple a) -&gt; a.getWeight() | Apple::getWeight

() -&gt; Thread.currentThread().dumpStack() | Thread.currentThread()::dumpStack

(str, i) -&gt; str.substring(i) | String::substring

(String s) -&gt; System.out.println(s) | System.out::println

五、流的中间操作

| 操作 | 类型 | 返回类型 | 操作参数 | 函数描述符 |

| -------- | ---- | ----------- | ---------------- | -------------- |

| filter | 中间 | Stream&lt;T&gt; | Predicate&lt;T&gt; | T -&gt; boolean |

| map | 中间 | Stream&lt;R&gt; | Function&lt;T, R&gt; | T -&gt; R |

| limit | 中间 | Stream&lt;T&gt; | long | |

| skip | 中间 | Stream&lt;T&gt; | long | |

| sorted | 中间 | Stream&lt;T&gt; | Comparator&lt;T&gt; | (T, T) -&gt; int |

| distinct | 中间 | Stream&lt;T&gt; | | |

六、流的终端操作

| 操作 | 类型 | 目的 |

| ------- | ---- | ---------------------------------------------------- |

| forEach | 终端 | 消费流中的每个元素并对其应用Lambda。这一操作返回void |

| count | 终端 | 返回流中元素的个数。这一操作返回long |

| collect | 终端 | 把流归约成一个集合,比如List、Map 甚至是Integer。 |

七、中间操作和终端操作

| 操作 | 类型 | 返回类型 | 使用的类型/函数式接口 | 函数描述符 |

| --------- | ---- | ------------- | -------------------------- | ----------------- |

| filter | 中间 | Stream&lt;T&gt; | Predicate&lt;T&gt; | T -&gt; boolean |

| distinct | 中间 | Stream&lt;T&gt; | | |

| skip | 中间 | Stream&lt;T&gt; | long | |

| limit | 中间 | Stream&lt;T&gt; | long | |

| map | 中间 | Stream&lt;R&gt; | Function&lt;T, R&gt; | T -&gt; R |

| flatMap | 中间 | Stream&lt;R&gt; | Function&lt;T, Stream&lt;R&gt;&gt; | T -&gt; Stream&lt;R&gt; |

| sorted | 中间 | Stream&lt;T&gt; | Comparator&lt;T&gt; | (T, T) -&gt; int |

| anyMatch | 终端 | boolean | Predicate&lt;T&gt; | T -&gt; boolean |

| noneMatch | 终端 | boolean | Predicate&lt;T&gt; | T -&gt; boolean |

| allMatch | 终端 | boolean | Predicate&lt;T&gt; | T -&gt; boolean |

| findAny | 终端 | Optional&lt;T&gt; | | |

| findFirst | 终端 | Optional&lt;T&gt; | | |

| forEach | 终端 | void | Consumer&lt;T&gt; | T -&gt; void |

| collect | 终端 | R | Collector&lt;T, A, R&gt; | |

| reduce | 终端 | Optional | | |