java8中lambda表达式使用

java8中的lambda表达式的常见使用. 确实好用.

lambda本质是语法糖. 帮我们简化了好多代码量.

说白了就是一个带输入参数的可执行块. (Function)

先看代码吧:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
System.out.println("************");

// foreach
List<String> strings = Arrays.asList("hello", "world", "helloworld", "hello");
strings.forEach(elem -> {
System.out.println(elem);
});

System.out.println("************");

输出:
************
hello
world
helloworld
hello
************
1
2
3
4
5
6
7
8
9
10
11
System.out.println("************");

// map
List stringList = strings.stream().map(elem -> elem + " dottie ").collect(Collectors.toList());
System.out.println(stringList);

System.out.println("************");
输出:
************
[hello dottie , world dottie , helloworld dottie , hello dottie ]
************
1
2
3
4
5
6
7
8
9
10
11
System.out.println("************");

// filter
List stringsList = strings.stream().filter((elem) -> elem.length() >= 5 && elem.startsWith("h")).collect(Collectors.toList());
System.out.println(stringsList);

System.out.println("************");
输出:
************
[hello, helloworld, hello]
************
1
2
3
4
5
6
7
8
9
10
System.out.println("************");

// map + foreach
String[] array = {"a", "b", "c"};
Stream.of(array).map(elem -> elem + " ").forEach(elem -> System.out.print(elem));

System.out.println("************");
输出:
************
a b c ************
1
2
3
4
5
6
7
8
9
10
11
12
System.out.println("************");

// distinct
strings.stream().distinct().forEach(elem -> System.out.println(elem));

System.out.println("************");
输出:
************
hello
world
helloworld
************
1
2
3
4
5
6
7
8
9
10
11
System.out.println("************");

// limit
strings.stream().limit(2).forEach(elem -> System.out.println(elem));

System.out.println("************");
输出:
************
hello
world
************
1
2
3
4
5
6
7
8
9
10
11
System.out.println("************");

// skip
strings.stream().skip(2).forEach(elem -> System.out.println(elem));

System.out.println("************");
输出:
************
helloworld
hello
************
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
System.out.println("************");

// peek
strings.stream().peek(elem -> System.out.println(elem + " 123")).forEach(elem -> System.out.println(elem));

System.out.println("*************");
输出:
************
hello 123
hello
world 123
world
helloworld 123
helloworld
hello 123
hello
*************
1
2
3
4
5
6
7
8
// reduce
System.out.println("*************");
String s = strings.stream().reduce((sumElem, elem) -> sumElem + elem).get();
System.out.println(s);
System.out.println("*************");
*************
helloworldhelloworldhello
*************

代码解释:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* 可以将stream看做是一个高级版本的iterator.
* 使用stream的步骤:
* 1. 创建stream
* 2. 转换stream, 每次转换时 原有stream对象不改变, 返回一个新的stream对象(**可有多次转换.**)
* 3. 对streamji进行聚合(Reduce)操作, 获取想要的结果.
*
*
* 1. 创建stream的两种办法
* -1. 通过Stream接口中的静态方法(java8中接口中可以定义静态方法);
* 常用的就是Stream.of(T), 有两个重载的方法.
* -2. 通过Collection子类获取Stream对象.
*
* 2. 转换stream. 即通过某些行为转换成一个新的stream.
* 介绍几个常用的转换方法:
* -1. distinct: 对元素去重.
* -2. filter: 对stream中包含的元素使用指定的过滤函数进行过滤操作.
* -3. map: 对stream中包含的元素使用指定函数进行转换操作.
* 这个方法有将stream中的元素转换成原始类型的三个方法:
* -1. mapToInt, mapToDouble, mapToLong, 可以免除自动装箱/拆箱的额外消耗.
* -4. flatMap:和map类似,不同的是其每个元素转换得到的是Stream对象,会把子Stream中的元素压缩到父集合中;
* -5. limit: 对一个Stream进行截断操作,获取其前N个元素,如果原Stream中包含的元素个数小于N,那就获取其所有的元素;
* -6. peek: 生成一个包含原Stream的所有元素的新Stream,同时会提供一个消费函数(Consumer实例),新Stream每个元素被消费的时候都会执行给定的消费函数;
*
* 注意: 当然这些转换方法是可以串联同时使用的.
*
* 3. Reduce(fold)折叠操作. 把序列中的元素合并成一个汇总的结果
* -1. 可变汇聚:把输入的元素们累积到一个可变的容器中,比如Collection或者StringBuilder;
-2. 其他汇聚:除去可变汇聚剩下的,一般都不是通过反复修改某个可变对象,而是通过把前一次的汇聚结果当成下一次的入参,反复如此。比如reduce,count,allMatch;
reduce()返回值是一个Optional对象.
*
*/