Commit 7bce761
committed
feat(streams-collectors): add comprehensive demo of Java Collectors API with real-world patterns
What
- Introduced `Test.java` showcasing advanced usage of the `Collectors` utility in the Streams API.
- Demonstrated 13 collector patterns with comments and examples:
1. toList → collect filtered results into a List.
2. toSet → unique collection, removes duplicates.
3. toCollection → collect into a specific collection (ArrayDeque).
4. joining → concatenate stream elements into a String with delimiter.
5. summarizingInt → compute count, sum, min, average, max.
6. averagingInt → directly calculate averages.
7. counting → count total elements.
8. groupingBy → classify/group by function (length, etc.), with downstream collectors.
9. partitioningBy → split into two groups by predicate (true/false).
10. mapping → transform before collecting.
11. toMap → create Map with key/value mappers.
12. toMap with merge function → handle duplicate keys (frequency counter).
13. reduce vs summingInt → comparison of manual vs collector-based summation.
- Added extensive inline notes + revision guide at the end of file.
Why
- Collectors are a core part of Java’s functional style introduced in Java 8.
- Understanding different collectors is essential for:
- Data aggregation (counts, averages, groupings).
- Data transformation pipelines (filtering, mapping).
- Preparing data for APIs, reports, or dashboards.
- This example acts as both a coding reference and revision guide for interviews or project work.
Logic
1. **Collection Operations**
- Used toList, toSet, toCollection to demonstrate how streams can materialize results in different container types.
- Ensures developers see the difference between ordered duplicates (List) vs unique (Set).
2. **String & Numeric Summarization**
- joining() simplifies concatenation of stream elements.
- summarizingInt() and averagingInt() abstract common statistical operations, reducing boilerplate.
3. **Counting & Partitioning**
- counting() is shorthand for size.
- partitioningBy() is a specialized grouping for boolean conditions, ideal for classification tasks.
4. **Grouping & Mapping**
- groupingBy() allows flexible downstream collectors (joining, counting).
- mapping() provides transformation during collection stage.
5. **Map Creation & Frequency Analysis**
- toMap() builds dictionaries (key → value).
- Merge function `(x, y) -> x + y` elegantly counts word frequencies, replacing manual loops.
6. **Reduction vs Collector**
- Showed reduce() vs summingInt() for summation:
- reduce → general-purpose but verbose.
- summingInt → cleaner, purpose-built for sums.
Real-life applications
- **Analytics & Reporting**: group employees by department, calculate averages, prepare dashboards.
- **Log Processing**: count word/phrase occurrences, group by severity levels.
- **Finance & E-commerce**: partition transactions (fraudulent vs safe), calculate order totals.
- **APIs & Microservices**: transform backend data into DTO maps for response payloads.
- **Data Science/ETL Pipelines**: summarize, clean, and aggregate large datasets with declarative pipelines.
Notes
- Collectors significantly reduce boilerplate (no explicit loops, maps).
- Declarative style improves readability and maintainability.
- groupingBy + downstream collectors = one of the most powerful patterns in stream processing.
- This file doubles as a "Collectors revision cheat-sheet."
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 2e3531b commit 7bce761
1 file changed
+160
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 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 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
0 commit comments