forked from AndrewProgramming/JavaTutorialCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamReductionDemo.java
More file actions
35 lines (26 loc) · 1.11 KB
/
StreamReductionDemo.java
File metadata and controls
35 lines (26 loc) · 1.11 KB
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
package stream;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class StreamReductionDemo {
public static void main(String[] args) {
int[] values = {3, 4, 1, 5, 20, 1, 3, 3, 4, 6};
System.out.print("The values are ");
IntStream.of(values).forEach(e -> System.out.print(e + " "));
System.out.println("\nThe result of multiplying all values is " +
IntStream.of(values).parallel().reduce(1, (e1, e2) -> e1 * e2));
System.out.print("The values are " +
IntStream.of(values).mapToObj(e -> e + "")
.reduce((e1, e2) -> e1 + ", " + e2).get());
String[] names = {"John", "Peter", "Susan", "Kim", "Jen",
"George", "Alan", "Stacy", "Michelle", "john"};
System.out.print("\nThe names are: ");
System.out.println(Stream.of(names)
.reduce((x, y) -> x + ", " + y).get());
System.out.print("Concat names: ");
System.out.println(Stream.of(names)
.reduce((x, y) -> x + y).get());
System.out.print("Total number of characters: ");
System.out.println(Stream.of(names)
.reduce((x, y) -> x + y).get().length());
}
}