-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStreamAPI.java
65 lines (55 loc) · 1.79 KB
/
StreamAPI.java
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.diyishuai.java8.stream;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* Created by Bruce on 16/8/9.
*
* filter 筛选
* map流映射
* reduce 将流中的元素组合起来
* collect 返回集合
* sorted 排序
* flatMap 流转换
* limit返回指定流个数
* distinct去除重复元素
*/
public class StreamAPI {
public static void main(String[] args) {
System.out.println("foreach-less1.8");
//foreach-less1.8
List<String> strings = Arrays.asList("a","b","c","c");
for (String str:strings) {
System.out.println(str);
}
System.out.println("-------");
System.out.println("foreach 1.8");
//foreach 1.8
strings.forEach(str -> System.out.println(str));
// strings.forEach(str -> {System.out.println(str);});
System.out.println("===========");
//stream
strings.stream()
.distinct()
.limit(2)
.filter(str -> str == "a")
.forEach(str -> System.out.println(str));
System.out.println("===========");
System.out.println("peek");
strings.parallelStream()
.skip(2)
.forEach(str -> System.out.println(str));
System.out.println("===========");
distinctPrimary("12","23","12","23");
System.out.println("===========");
}
public static void distinctPrimary(String...numbers) {
List<String> strings = Arrays.asList(numbers);
List<Integer> collect = strings.parallelStream()
.map(e -> new Integer(e))
// .filter(e -> Primes.isPrime(e))
.distinct()
.collect(Collectors.toList());
System.out.println(collect);
}
}