-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamsAdvancedExample.java
More file actions
36 lines (28 loc) · 1.13 KB
/
Copy pathStreamsAdvancedExample.java
File metadata and controls
36 lines (28 loc) · 1.13 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
/**
* Day 28 - Streams API: Advanced Operations
*/
import java.util.*;
import java.util.stream.*;
public class StreamsAdvancedExample {
public static void main(String[] args) {
System.out.println("=== Advanced Stream Operations ===\n");
List<String> words = Arrays.asList("Java", "Python", "JavaScript", "C++", "Go");
System.out.println("--- Sorted ---");
words.stream()
.sorted()
.forEach(System.out::println);
System.out.println("\n--- Sorted by Length ---");
words.stream()
.sorted(Comparator.comparing(String::length))
.forEach(System.out::println);
System.out.println("\n--- Count ---");
long count = words.stream()
.filter(w -> w.length() > 4)
.count();
System.out.println("Words with length > 4: " + count);
System.out.println("\n--- Convert to Map ---");
Map<String, Integer> wordLengths = words.stream()
.collect(Collectors.toMap(w -> w, String::length));
wordLengths.forEach((k, v) -> System.out.println(k + ": " + v));
}
}