Skip to content

Commit 3af50f4

Browse files
committed
Method Reference and Stream, Parallel Streams
1 parent 73677ad commit 3af50f4

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.arpit.java8.method.reference;
2+
3+
@FunctionalInterface
4+
public interface HelloService {
5+
6+
void hello();
7+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.arpit.java8.method.reference;
2+
3+
public class Main {
4+
5+
private static void hello() {
6+
System.out.println("hello");
7+
}
8+
9+
public static void main(String[] args) {
10+
// Referring static method
11+
HelloService helloService = Main::hello;
12+
// Calling interface method
13+
helloService.hello();
14+
}
15+
16+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.arpit.java8.streams;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class StreamAndParallelStream {
7+
8+
public static void main(String... args) {
9+
List<Integer> list = new ArrayList<>();
10+
for (int i = 0; i < 1000; i++) {
11+
list.add(i);
12+
}
13+
printUsingStream(list);
14+
printUsingParallelStream(list);
15+
}
16+
17+
private static void printUsingStream(List<Integer> list) {
18+
list.stream().forEach(System.out::println);
19+
}
20+
21+
/**
22+
* If we change stream() to parallelStream() this is not the case anymore
23+
* all number are written, but in a different order. So, apparently,
24+
* parallelStream() indeed uses multiple threads.
25+
*
26+
*
27+
* @param list
28+
*/
29+
private static void printUsingParallelStream(List<Integer> list) {
30+
list.parallelStream().forEach(System.out::println);
31+
}
32+
}

0 commit comments

Comments
 (0)