Skip to content

Commit 8180e70

Browse files
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 changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import java.util.Arrays;
2+
import java.util.List;
3+
import java.util.stream.Collectors;
4+
5+
public class Test {
6+
public static void main(String[] args) {
7+
List<Integer> list = Arrays.asList(1,2,3,4,5,6,6,7,8,8,9,4444,5,6,7,7,7,73454,0,1,1);
8+
9+
// Find square of all the numbers in the list.
10+
List<Integer> squaredList =
11+
list.stream()
12+
.map(x -> x * x) // map() transforms each element → x^2
13+
.collect(Collectors.toList());
14+
System.out.println("Squares: " + squaredList);
15+
16+
17+
// Printing even numbers from the list.
18+
List<Integer> FilteredList =
19+
list.stream().
20+
filter(x -> x % 2 == 0).
21+
collect(Collectors.toList());
22+
//x -> x % 2 == 0 predicate likha hai.
23+
System.out.println(FilteredList);
24+
25+
26+
// Every even number divide by 2.
27+
List<Integer> MappedList =
28+
FilteredList.stream().
29+
map(x -> x / 2).
30+
collect(Collectors.toList());
31+
System.out.println(MappedList);
32+
33+
List<Integer> MappedAlternative =
34+
list.stream().
35+
filter(x -> x % 2 == 0).
36+
map(x -> x / 2).
37+
collect(Collectors.toList());
38+
System.out.println(MappedAlternative);
39+
40+
List<Integer> MappedDistinct =
41+
list.stream().
42+
filter(x -> x % 2 == 0).
43+
map(x -> x / 2).
44+
distinct().
45+
collect(Collectors.toList());
46+
System.out.println(MappedDistinct);
47+
48+
List<Integer> MappedSorted =
49+
list.stream().
50+
filter(x -> x % 2 == 0).
51+
map(x -> x / 2).
52+
distinct().sorted().
53+
collect(Collectors.toList());
54+
System.out.println(MappedSorted);
55+
56+
List<Integer> MappedSortToYourOwnNotNaturalOrderSorting =
57+
list.stream().
58+
filter(x -> x % 2 == 0).map(x -> x / 2).
59+
distinct().sorted((a, b) -> (b - a)).
60+
collect(Collectors.toList());
61+
62+
System.out.println(MappedSortToYourOwnNotNaturalOrderSorting + "Custom Sorting");
63+
64+
List<Integer> CustomSort =
65+
list.stream().
66+
filter(x -> x % 2 == 0).map(x -> x / 2).
67+
distinct().sorted((a, b) -> (b - a)).
68+
limit(4).
69+
collect(Collectors.toList());
70+
71+
System.out.println(CustomSort + "Custom Sorting Only show 4 element");
72+
73+
List<Integer> Limit5 =
74+
list.stream().
75+
filter(x -> x % 2 == 0).map(x -> x / 2).
76+
distinct().sorted((a, b) -> (b - a)).
77+
limit(4).
78+
skip(1).
79+
collect(Collectors.toList());
80+
81+
System.out.println(Limit5);
82+
}
83+
}

0 commit comments

Comments
 (0)