Skip to content

Commit e96f9fd

Browse files
committed
📝 update stream example
1 parent 1aa3cdd commit e96f9fd

File tree

1 file changed

+18
-14
lines changed
  • java8-stream/src/main/java/io/github/biezhi/java8/stream/lesson3

1 file changed

+18
-14
lines changed
Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package io.github.biezhi.java8.stream.lesson3;
22

3+
import java.util.ArrayList;
34
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.UUID;
7+
import java.util.concurrent.TimeUnit;
48

59
/**
610
* 并行流
@@ -11,19 +15,19 @@
1115
public class Example6 {
1216

1317
public static void main(String[] args) {
14-
Arrays.asList("a1", "a2", "b1", "c2", "c1")
15-
.parallelStream()
16-
.filter(s -> {
17-
System.out.format("filter: %s [%s]\n",
18-
s, Thread.currentThread().getName());
19-
return true;
20-
})
21-
.map(s -> {
22-
System.out.format("map: %s [%s]\n",
23-
s, Thread.currentThread().getName());
24-
return s.toUpperCase();
25-
})
26-
.forEach(s -> System.out.format("forEach: %s [%s]\n",
27-
s, Thread.currentThread().getName()));
18+
int max = 1000000;
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+
long t0 = System.nanoTime();
26+
long count = values.stream().sorted().count();
27+
System.out.println(count);
28+
long t1 = System.nanoTime();
29+
long millis = TimeUnit.NANOSECONDS.toMillis(t1 - t0);
30+
System.out.println(String.format("sequential sort took: %d ms", millis));
31+
2832
}
2933
}

0 commit comments

Comments
 (0)