-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectorsDemo.java
More file actions
103 lines (78 loc) · 4.53 KB
/
CollectorsDemo.java
File metadata and controls
103 lines (78 loc) · 4.53 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package Stream;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author VIVEK
* Collectors is a utility class
* provides a set of methods to create common collectors
*/
public class CollectorsDemo {
static void main(String[] args) {
// 1. Collecting to a List
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
List<String> res = names.stream().collect(Collectors.toList());
// 2. Collecting to a Set
List<Integer> nums = Arrays.asList(1, 2, 2, 3, 4, 4, 5);
Set<Integer> set = nums.stream().collect(Collectors.toSet());
System.out.println(set);
// 3. Collecting to a Specific Collection
ArrayDeque<Integer> deque = nums.stream().collect(Collectors.toCollection(ArrayDeque::new));
// 4. Joining Strings
// Concatenates stream elements into a single String
System.out.println(names.stream().collect(Collectors.joining(", ")));
// 5. Summarizing Data
// Generates statistical summary (count, sum, min, average, max)
List<Integer> numbers = Arrays.asList(2, 3, 5, 7, 11);
IntSummaryStatistics stats = numbers.stream().collect(Collectors.summarizingInt(n -> n));
System.out.println("Count: " + stats.getCount());
System.out.println("Sum: " + stats.getSum());
System.out.println("Min: " + stats.getMin());
System.out.println("Average: " + stats.getAverage());
System.out.println("Max: " + stats.getMax());
// 6. Calculating Averages
Double average = numbers.stream().collect(Collectors.averagingInt(x -> x));
System.out.println("Average: " + average);
// 7. Counting Elements
Long count = numbers.stream().collect(Collectors.counting());
System.out.println("Count: " + count);
// 8. Grouping Elements
List<String> words = Arrays.asList("hello", "world", "java", "streams", "collecting");
System.out.println(words.stream().collect(Collectors.groupingBy(String::length)));
System.out.println(words.stream().collect(Collectors.groupingBy(String::length, Collectors.joining(", "))));
System.out.println(words.stream().collect(Collectors.groupingBy(String::length, Collectors.counting())));
LinkedHashMap<Integer, Long> linkedHashMap = words.stream().collect(Collectors.groupingBy(String::length
, LinkedHashMap::new, Collectors.counting()));
System.out.println(linkedHashMap);
// 9. Partitioning Elements
// Partitions elements into two groups (true and false) based on a predicate
System.out.println(words.stream().collect(Collectors.partitioningBy(s -> s.length() > 6)));
System.out.println(words.stream().collect(Collectors.partitioningBy(s -> s.length() > 6, Collectors.counting())));
// 10. Mapping and Collecting
// Applies a mapping function before collecting
System.out.println(words.stream().collect(Collectors.mapping(String::toUpperCase, Collectors.toList())));
// 11. toMap
// Example 1: Collecting Names by Length
List<String> l1 = Arrays.asList("Anna", "Bob", "Alexander", "Brian", "Alice");
System.out.println(l1.stream().collect(Collectors.groupingBy(String::length)));
// Example 2: Counting Word Occurrences
String sentence = "hello world hello java world";
System.out.println(Arrays.stream(sentence.split(" ")).collect(Collectors.groupingBy(x -> x,
Collectors.counting())));
// Example 3: Partitioning Even and Odd Numbers
List<Integer> l2 = Arrays.asList(1, 2, 3, 4, 5, 6);
System.out.println(l2.stream().collect(Collectors.partitioningBy(x -> (x & 1) == 0)));
// Example 4: Summing Values in a Map
Map<String, Integer> items = new HashMap<>();
items.put("Apple", 10);
items.put("Banana", 20);
items.put("Orange", 15);
System.out.println(items.values().stream().reduce(Integer::sum).orElse(0));
System.out.println(items.values().stream().collect(Collectors.summingInt(Integer::intValue)));
// Example 5: Creating a Map from Stream Elements
List<String> fruits = Arrays.asList("Apple", "Banana", "Cherry");
System.out.println(fruits.stream().collect(Collectors.toMap(String::toLowerCase, String::toUpperCase)));
// Example 6:
List<String> words2 = Arrays.asList("apple", "banana", "apple", "orange", "banana", "apple");
System.out.println(words2.stream().collect(Collectors.toMap(k -> k, v -> 1, (x, y) -> x + y)));
}
}