-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamAPI.java
More file actions
40 lines (32 loc) · 876 Bytes
/
StreamAPI.java
File metadata and controls
40 lines (32 loc) · 876 Bytes
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
package java8.stream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
public class StreamAPI {
public static void main(String[] args) {
System.out.println("Java7 : " + collectionJava7());
System.out.println("Java8 : " + collectionJava8());
}
private static long collectionJava7() {
List<Integer> integers = new ArrayList<>();
long count = 0;
for (int i = 0; i < 50; ++i) {
integers.add(i);
}
for (int i = 0; i < 50; ++i) {
integers.set(i, integers.get(i) * 2);
}
for (int i = 0; i < 50; ++i) {
if (integers.get(i) % 3 == 0) {
count++;
}
}
return count;
}
private static long collectionJava8() {
return IntStream.range(0, 50)
.mapToObj(value -> value * 2)
.filter(value -> value % 3 == 0)
.count();
}
}