-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamsExample.java
More file actions
36 lines (28 loc) · 1010 Bytes
/
StreamsExample.java
File metadata and controls
36 lines (28 loc) · 1010 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
/**
* Day 28 - Streams API
*/
import java.util.*;
import java.util.stream.*;
public class StreamsExample {
public static void main(String[] args) {
System.out.println("=== Streams API ===\n");
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
System.out.println("--- Filter ---");
numbers.stream()
.filter(n -> n % 2 == 0)
.forEach(System.out::println);
System.out.println("\n--- Map ---");
numbers.stream()
.map(n -> n * n)
.forEach(System.out::println);
System.out.println("\n--- Reduce ---");
int sum = numbers.stream()
.reduce(0, (a, b) -> a + b);
System.out.println("Sum: " + sum);
System.out.println("\n--- Collect ---");
List<Integer> doubled = numbers.stream()
.map(n -> n * 2)
.collect(Collectors.toList());
System.out.println("Doubled: " + doubled);
}
}