Commit b90028d
committed
feat(streams-api): add CollectorsDemo showcasing advanced collectors usage
What
- Introduced `CollectorsDemo` class demonstrating practical use-cases of the Java Streams API and Collectors.
- Added multiple examples:
1. Grouping elements by string length.
2. Counting word occurrences in a sentence.
3. Partitioning numbers into even/odd.
4. Summing values from a map.
5. Creating maps with transformations (uppercase → length).
6. Counting frequency of words using `toMap` with merge function.
- Each example annotated with input, logic, and expected result.
Why
- Collectors are central to data aggregation and transformation in modern Java applications.
- Provides clear, practical demonstrations beyond simple `toList()` usage.
- Helps developers understand declarative data processing vs imperative loops.
Logic
1. **Grouping by length**
- `Collectors.groupingBy(String::length)` → Map<Integer, List<String>> where key = length.
- Example: ["Anna", "Bob", "Alexander"] → {3=[Bob], 4=[Anna], 9=[Alexander]}.
2. **Counting word occurrences**
- Sentence split into words → grouped by word → count aggregated with `Collectors.counting()`.
- Efficient for computing frequency tables.
3. **Partitioning numbers**
- `Collectors.partitioningBy(x -> x % 2 == 0)` → splits into two lists:
true = evens, false = odds.
- Example: [1,2,3,4,5,6] → {true=[2,4,6], false=[1,3,5]}.
4. **Summing values**
- Demonstrated two ways:
- `reduce(Integer::sum)` for manual accumulation.
- `Collectors.summingInt(x -> x)` for cleaner summation.
- Example: {Apple=10, Banana=20, Orange=15} → 45.
5. **Creating a map with transformations**
- `Collectors.toMap(keyMapper, valueMapper)` builds maps directly from streams.
- Example: ["Apple", "Banana"] → {APPLE=5, BANANA=6}.
6. **Counting frequency using merge function**
- `Collectors.toMap(k -> k, v -> 1, (x,y)->x+y)` handles duplicate keys by summing counts.
- Example: ["apple","banana","apple"] → {apple=2, banana=1}.
Real-life applications
- **Analytics**: word frequency counters, log aggregations, grouping by attributes.
- **Reporting systems**: partitioning datasets (active/inactive users, passed/failed tests).
- **E-commerce apps**: summing cart values, grouping orders by customer, counting product sales.
- **Data transformation pipelines**: converting raw text or collections into structured maps.
Notes
- `groupingBy` → flexible grouping by classifier function.
- `partitioningBy` → boolean-based split, always returns two groups.
- `toMap` requires merge function when duplicates exist.
- `Collectors.summingInt` is cleaner and safer than manual reduce for summation.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent f4bd6c3 commit b90028d
File tree
2 files changed
+75
-112
lines changed- JAVA8/JavaCollectorsAPI/src
- Java 8 Crash Course/Java Collectors API/src
2 files changed
+75
-112
lines changedThis file was deleted.
Lines changed: 75 additions & 0 deletions
| 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 | + | |
0 commit comments