Java Lambda级联表达式
又称柯里化,把多个参数的函数转化为只有一个参数的多个函数级联的形式,目的是函数标准化。
Function<Integer, Function<Integer, Integer>> fun = x -> y -> x + y;
System.out.println(fun.apply(2).apply(3)); // 5
Stream流操作
创建
|
相关方法 |
集合 |
Collenction.stream/paralleStream |
数组 |
Array.stream、Stream.of(Array) |
数字Stream |
IntStream/LongStream.range/rangeClosed |
|
Random.ints/longs/doubles |
自己创建 |
Stream.generate/iterate |
中间操作
|
相关方法的 |
无状态操作 |
map/mapToXxx |
|
flatMap/flatMapToXxx |
|
filter |
|
peek |
|
unordered |
有状态操作 |
distinct |
|
sorted |
|
limit/skip |
终止操作
|
相关方法 |
非短路操作 |
forEach/forEachOrderd |
|
collect/toArray |
|
reduce |
|
min/max/count |
短路操作 |
findFirst/findAny |
|
allMatch/anyMatch/noneMatch |
例子
String str = "my name is 007";
// 找出单词大于2的单词字母数
Stream.of(str.split(" "))
.filter(s -> s.length() > 2)
.map(s -> s.length())
.forEach(System.out::println); // 4 3
// 转换为一个一个字母进行输出
Stream.of(str.split(" "))
.flatMap(s -> s.chars().boxed())
.forEach(i -> System.out.println((char) i.intValue()));