Skip to content

Commit 96a3e1e

Browse files
committed
Stream类
1 parent 27ebf68 commit 96a3e1e

File tree

6 files changed

+368
-4
lines changed

6 files changed

+368
-4
lines changed

src/main/java/com/xu/java8/stream/Optional1.java renamed to src/main/java/com/xu/java8/Optional/Optional1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.xu.java8.stream;
1+
package com.xu.java8.Optional;
22

33
import com.xu.java8.lambda.Person;
44

src/main/java/com/xu/java8/stream/Optional2.java renamed to src/main/java/com/xu/java8/Optional/Optional2.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
package com.xu.java8.stream;
1+
package com.xu.java8.Optional;
22

33
import java.util.Optional;
44
import java.util.function.Supplier;
55

66
/**
7-
* Created by xu on 2016/8/31.
7+
* Class java8-Optional类:解决空引用的问题
8+
*
9+
* @author hua xu
10+
* @version 1.0.0
11+
* @date 16/08/31
812
*/
913
public class Optional2 {
1014
static class Outer {
@@ -61,7 +65,7 @@ private static void test2() {
6165
}
6266

6367
private static void test1() {
64-
Optional<Outer> optional=Optional.of(new Outer());
68+
Optional<Outer> optional = Optional.of(new Outer());
6569
optional.flatMap(o -> Optional.ofNullable(o.nested))
6670
.flatMap(n -> Optional.ofNullable(n.inner))
6771
.flatMap(i -> Optional.ofNullable(i.foo))
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.xu.java8.Stream;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Optional;
6+
7+
/**
8+
* Class java8-Stream接口
9+
*
10+
* @author hua xu
11+
* @version 1.0.0
12+
* @date 16/08/31
13+
*/
14+
public class Streams1 {
15+
16+
public static void main(String[] args) {
17+
18+
/**
19+
* java.util.Stream 表示能应用在一组元素上一次执行的操作序列。
20+
* Stream 操作分为中间操作或者最终操作两种,最终操作返回一特定类型的计算结果,
21+
* 而中间操作返回Stream本身,这样你就可以将多个操作依次串起来。Stream 的
22+
* 创建需要指定一个数据源,比如 java.util.Collection的子类,List或者Set,
23+
* Map不支持。Stream的操作可以串行执行或者并行执行。
24+
*/
25+
List<String> stringCollection = new ArrayList<>();
26+
stringCollection.add("ddd2");
27+
stringCollection.add("aaa2");
28+
stringCollection.add("bbb1");
29+
stringCollection.add("aaa1");
30+
stringCollection.add("bbb3");
31+
stringCollection.add("ccc");
32+
stringCollection.add("bbb2");
33+
stringCollection.add("ddd1");
34+
35+
/**
36+
* Filter 过滤.
37+
* 过滤通过一个predicate接口来过滤并只保留符合条件的元素,该操作属于中间操作,
38+
* 所以我们可以在过滤后的结果来应用其他Stream操作(比如forEach)。forEach
39+
* 需要一个函数来对过滤后的元素依次执行。forEach是一个最终操作,所以我们不
40+
* 能在forEach之后来执行其他Stream操作。
41+
*/
42+
System.out.println("--------------filtering---------------");
43+
stringCollection
44+
.stream()
45+
.filter((s) -> s.startsWith("a"))
46+
.forEach(System.out::println);
47+
48+
/**
49+
* sorted 排序.
50+
* 是一个中间操作,返回的是排序好后的Stream。如果你不指定一个自定义的Comparator则会使用默认排序。
51+
*/
52+
System.out.println("--------------sorting-----------------");
53+
stringCollection
54+
.stream()
55+
.sorted()
56+
.forEach(System.out::println);
57+
System.out.println(stringCollection);//原数据源不会被改变
58+
59+
/**
60+
* 中间操作map会将元素根据指定的Function接口来依次将元素转成另外的对象
61+
*/
62+
System.out.println("--------------mapping-----------------");
63+
stringCollection
64+
.stream()
65+
.map(String::toUpperCase)
66+
.map((s)->s+" space")
67+
.sorted((a, b) -> b.compareTo(a))
68+
.forEach(System.out::println);
69+
70+
/**
71+
* Stream提供了多种匹配操作,允许检测指定的Predicate是否匹配整个Stream。
72+
* 所有的匹配操作都是最终操作,并返回一个boolean类型的值。
73+
*/
74+
System.out.println("--------------matching----------------");
75+
boolean anyStartsWithA = stringCollection
76+
.stream()
77+
.anyMatch((s) -> s.startsWith("a"));
78+
System.out.println(anyStartsWithA); // true
79+
80+
boolean allStartsWithA = stringCollection
81+
.stream()
82+
.allMatch((s) -> s.startsWith("a"));
83+
System.out.println(allStartsWithA); // false
84+
85+
boolean noneStartsWithZ = stringCollection
86+
.stream()
87+
.noneMatch((s) -> s.startsWith("z"));
88+
System.out.println(noneStartsWithZ); // true
89+
90+
/**
91+
* 计数是一个最终操作,返回Stream中元素的个数,返回值类型是long。
92+
*/
93+
System.out.println("--------------counting----------------");
94+
long startsWithB = stringCollection
95+
.stream()
96+
.filter((s) -> s.startsWith("b"))
97+
.count();
98+
System.out.println(startsWithB); // 3
99+
100+
/**
101+
* 这是一个最终操作,允许通过指定的函数来讲stream中的多个元素规约为一个元素,规越后的结果是通过Optional接口表示的
102+
*/
103+
System.out.println("--------------reducing----------------");
104+
Optional<String> reduced =
105+
stringCollection
106+
.stream()
107+
.sorted()
108+
.reduce((s1, s2) -> s1 + "#" + s2);
109+
110+
reduced.ifPresent(System.out::println);
111+
// "aaa1#aaa2#bbb1#bbb2#bbb3#ccc#ddd1#ddd2"
112+
}
113+
114+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.xu.java8.Stream;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.UUID;
6+
import java.util.concurrent.TimeUnit;
7+
8+
/**
9+
* Class java8-并行stream和串行stream比较
10+
*
11+
* @author hua xu
12+
* @version 1.0.0
13+
* @date 16/08/31
14+
*/
15+
public class Streams3 {
16+
public static final int MAX = 1000000;
17+
18+
public static void sortSequential() {
19+
List<String> values = new ArrayList<>(MAX);
20+
for (int i = 0; i < MAX; i++) {
21+
UUID uuid = UUID.randomUUID();
22+
values.add(uuid.toString());
23+
}
24+
25+
// sequential
26+
27+
long t0 = System.nanoTime();
28+
29+
long count = values.stream().sorted().count();
30+
System.out.println(count);
31+
32+
long t1 = System.nanoTime();
33+
34+
long millis = TimeUnit.NANOSECONDS.toMillis(t1 - t0);
35+
System.out.println(String.format("sequential sort took: %d ms", millis));
36+
}
37+
38+
public static void sortParallel() {
39+
List<String> values = new ArrayList<>(MAX);
40+
for (int i = 0; i < MAX; i++) {
41+
UUID uuid = UUID.randomUUID();
42+
values.add(uuid.toString());
43+
}
44+
45+
// sequential
46+
47+
long t0 = System.nanoTime();
48+
49+
long count = values.parallelStream().sorted().count();
50+
System.out.println(count);
51+
52+
long t1 = System.nanoTime();
53+
54+
long millis = TimeUnit.NANOSECONDS.toMillis(t1 - t0);
55+
System.out.println(String.format("parallel sort took: %d ms", millis));
56+
}
57+
58+
public static void main(String[] args) {
59+
sortSequential();
60+
sortParallel();
61+
}
62+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.xu.java8.Stream;
2+
3+
import java.util.OptionalInt;
4+
import java.util.stream.IntStream;
5+
6+
/**
7+
* Class IntStream接口
8+
*
9+
* @author hua xu
10+
* @version 1.0.0
11+
* @date 16/08/31
12+
*/
13+
public class Streams4 {
14+
public static void main(String[] args) {
15+
16+
for (int i = 0; i < 10; i++) {
17+
if (i % 2 == 1) {
18+
System.out.print(i+" ");
19+
}
20+
}
21+
22+
System.out.println();
23+
IntStream.range(0, 10)
24+
.forEach(i -> {
25+
if (i % 2 == 1) System.out.print(i+" ");
26+
});
27+
28+
System.out.println();
29+
IntStream.range(0, 10)
30+
.filter(i -> i % 2 == 1)
31+
.forEach((i)-> System.out.print(i+" "));
32+
33+
System.out.println();
34+
OptionalInt reduced1 =
35+
IntStream.range(0, 10)
36+
.reduce((a, b) -> a + b);
37+
System.out.println(reduced1.getAsInt());
38+
39+
int reduced2 =
40+
IntStream.range(0, 10)
41+
.reduce(7, (a, b) -> a + b);
42+
System.out.println(reduced2);
43+
}
44+
}
45+
46+
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package com.xu.java8.Stream;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.function.Supplier;
6+
import java.util.stream.Stream;
7+
8+
/**
9+
* stream过程中的操作
10+
*/
11+
public class Streams5 {
12+
13+
public static void main(String[] args) {
14+
List<String> strings =
15+
Arrays.asList("d2", "a2", "b1", "b3", "c");
16+
17+
//test1(strings);
18+
//test2(strings);
19+
//test3(strings);
20+
// test4(strings);
21+
// test5(strings);
22+
//test6(strings);
23+
//test7(strings);
24+
test8(strings);
25+
}
26+
27+
private static void test8(List<String> stringCollection) {
28+
Supplier<Stream<String>> streamSupplier =
29+
() -> stringCollection
30+
.stream()
31+
.filter(s -> s.startsWith("a"));
32+
33+
streamSupplier.get().anyMatch(s -> true);
34+
streamSupplier.get().noneMatch(s -> true);
35+
}
36+
37+
38+
private static void test7(List<String> stringCollection) {
39+
Stream<String> stream = stringCollection
40+
.stream()
41+
.filter(s -> s.startsWith("a"));
42+
43+
System.out.println(stream.anyMatch(s -> true));
44+
45+
/**stream has already been operated upon or closed*/
46+
System.out.println(stream.noneMatch(s -> true));
47+
}
48+
49+
// short-circuit
50+
private static void test6(List<String> stringCollection) {
51+
stringCollection
52+
.stream()
53+
.map(s -> {
54+
System.out.println("map: " + s);
55+
return s.toUpperCase();
56+
})
57+
.anyMatch(s -> {
58+
System.out.println("anyMatch: " + s);
59+
return s.startsWith("A");
60+
});
61+
}
62+
63+
private static void test5(List<String> stringCollection) {
64+
stringCollection
65+
.stream()
66+
.filter(s -> {
67+
System.out.println("filter: " + s);
68+
return s.toLowerCase().startsWith("a");
69+
})
70+
.sorted((s1, s2) -> {
71+
System.out.printf("sort: %s; %s\n", s1, s2);
72+
return s1.compareTo(s2);
73+
})
74+
.map(s -> {
75+
System.out.println("map: " + s);
76+
return s.toUpperCase();
77+
})
78+
.forEach(s -> System.out.println("forEach: " + s));
79+
}
80+
81+
// sorted = horizontal
82+
private static void test4(List<String> stringCollection) {
83+
stringCollection
84+
.stream()
85+
.sorted((s1, s2) -> {
86+
System.out.printf("sort: %s; %s\n", s1, s2);
87+
return s1.compareTo(s2);
88+
})
89+
.filter(s -> {
90+
System.out.println("filter: " + s);
91+
return s.toLowerCase().startsWith("a");
92+
})
93+
.map(s -> {
94+
System.out.println("map: " + s);
95+
return s.toUpperCase();
96+
})
97+
.forEach(s -> System.out.println("forEach: " + s));
98+
}
99+
100+
private static void test3(List<String> stringCollection) {
101+
stringCollection
102+
.stream()
103+
.filter(s -> {
104+
System.out.println("filter: " + s);
105+
return s.startsWith("a");
106+
})
107+
.map(s -> {
108+
System.out.println("map: " + s);
109+
return s.toUpperCase();
110+
})
111+
.forEach(s -> System.out.println("forEach: " + s));
112+
}
113+
114+
private static void test2(List<String> stringCollection) {
115+
stringCollection
116+
.stream()
117+
.map(s -> {
118+
System.out.println("map: " + s);
119+
return s.toUpperCase();
120+
})
121+
.filter(s -> {
122+
System.out.println("filter: " + s);
123+
return s.startsWith("A");
124+
})
125+
.forEach(s -> System.out.println("forEach: " + s));
126+
}
127+
128+
private static void test1(List<String> stringCollection) {
129+
stringCollection
130+
.stream()
131+
.filter(s -> {
132+
System.out.println("filter: " + s);
133+
return true;
134+
})
135+
.forEach(s -> System.out.println("forEach: " + s));
136+
}
137+
138+
}

0 commit comments

Comments
 (0)