Commit 8180e70
committed
feat(streams): demonstrate advanced Stream API operations with mapping, filtering, sorting, and limiting
What
- Added `Test.java` showcasing various Stream API transformations and queries on a sample list of integers.
- Implemented multiple stream pipelines:
1. **Square numbers**
- Used `map(x -> x * x)` to compute squares.
- Collected into a new list for printing.
2. **Filter evens**
- Applied `filter(x -> x % 2 == 0)` to extract only even numbers.
3. **Divide evens by 2**
- First approach: filter → collect → map over filtered list.
- Alternative: filter + map in one pipeline.
- Showed both styles for clarity.
4. **Remove duplicates**
- Added `.distinct()` after mapping to eliminate repeated results.
5. **Sorting**
- Natural order sorting with `.sorted()`.
- Custom comparator `(b - a)` for descending order sorting.
6. **Limit and Skip**
- Used `.limit(4)` to restrict output.
- Chained `.skip(1)` to drop the first element after limiting.
Why
- Provides a **comprehensive walkthrough** of commonly used Stream API features:
- Transformation (map)
- Filtering (filter)
- Deduplication (distinct)
- Sorting (sorted + custom comparator)
- Subsetting (limit + skip)
- Useful as a reference for learning both **stateless** (map, filter) and **stateful** (distinct, sorted, limit, skip) operations.
Logic
1. **Mapping** → transforms data (square numbers, divide evens by 2).
2. **Filtering** → keeps only desired values (even numbers).
3. **Distinct** → removes duplicates, requires remembering seen values.
4. **Sorting**
- Natural order = ascending.
- Custom comparator = descending order (b - a).
5. **Limit + Skip** → subsetting stream results (pagination-like behavior).
6. Chaining operations shows how pipelines can be composed flexibly.
Key Takeaways
✔ `map` and `filter` are stateless (process elements independently).
✔ `distinct`, `sorted`, `limit`, `skip` are stateful (need global state or buffering).
✔ Stream pipelines can be tailored for transformation, deduplication, sorting, and subsetting.
✔ Custom comparators and chaining allow building powerful, expressive queries.
✔ Order of operations matters: filtering before mapping reduces unnecessary work.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent e3c4ef2 commit 8180e70
File tree
1 file changed
+83
-0
lines changed- Java 8 Crash Course/Java 8 Streams/StreamOperations/src
1 file changed
+83
-0
lines changedLines changed: 83 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 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
0 commit comments