šŸ”§ Core Stream Functions in Java Lambda Processing

1. šŸ”¹ map(Function<T, R>)

Purpose: Transforms each element into another form (1-to-1 mapping)

List<String> names = List.of("Amit", "Neha");
List<Integer> lengths = names.stream()
Ā  Ā  .map(String::length)
Ā  Ā  .collect(Collectors.toList());

Use Case: Convert objects, extract fields, modify values


2. šŸ”¹ filter(Predicate<T>)

Purpose: Filters elements that match a condition

List<Integer> nums = List.of(1, 2, 3, 4);
List<Integer> even = nums.stream()
Ā  Ā  .filter(n -> n % 2 == 0)
Ā  Ā  .collect(Collectors.toList());

Use Case: Keep only items that satisfy a condition


3. šŸ”¹ reduce(BinaryOperator<T>)

Purpose: Combines all stream elements into a single result

int sum = List.of(1, 2, 3).stream()
Ā  Ā  .reduce(0, Integer::sum); // Output: 6

Use Case: Calculate sum, product, max, concatenate, etc.


4. šŸ”¹ forEach(Consumer<T>)

Purpose: Performs an action for each element (terminal operation)

List.of("Java", "Python").forEach(System.out::println);

Use Case: Printing, updating external variables (use with caution in parallel streams)


5. šŸ”¹ collect(Collector)

Purpose: Converts stream to a collection or summarizing result

List<String> result = Stream.of("a", "b")
Ā  Ā  .collect(Collectors.toList());

Use Case: Build List, Set, Map, count elements, group by, etc.


6. šŸ”¹ sorted(), sorted(Comparator<T>)

Purpose: Sorts stream elements naturally or with a custom comparator

List.of(5, 3, 9).stream()
Ā  Ā  .sorted()
Ā  Ā  .forEach(System.out::print); // 3 5 9

Use Case: Sorting by value, property, custom logic


7. šŸ”¹ limit(n) & skip(n)

Purpose: Truncate or skip elements from the stream

Stream.of(1, 2, 3, 4, 5)
Ā  Ā  .skip(2)
Ā  Ā  .limit(2)
Ā  Ā  .forEach(System.out::println); // Output: 3 4

Use Case: Pagination, trimming input


8. šŸ”¹ distinct()

Purpose: Removes duplicate elements (based on equals())

Stream.of(1, 2, 2, 3)
Ā  Ā  .distinct()
Ā  Ā  .forEach(System.out::println); // 1 2 3

Use Case: Ensure uniqueness in a collection


9. šŸ”¹ flatMap(Function<T, Stream<R>>)

Purpose: Flattens nested streams into a single stream

List<List<String>> data = List.of(List.of("a", "b"), List.of("c"));
List<String> flat = data.stream()
Ā  Ā  .flatMap(List::stream)
Ā  Ā  .collect(Collectors.toList());

Use Case: Flatten nested lists, transform collections of collections


10. šŸ”¹ anyMatch, allMatch, noneMatch

Purpose: Checks for match conditions

boolean anyEven = List.of(1, 3, 5).stream()
Ā  Ā  .anyMatch(n -> n % 2 == 0); // false

Use Case: Validation, existence checks


11. šŸ”¹ findFirst(), findAny()

Purpose: Get the first or any available element (wrapped in Optional)

Optional<Integer> first = Stream.of(2, 4, 6).findFirst();

Use Case: Early exit lookup, search operations


šŸ“Œ Summary Table

Function Use Case
map() Transform each element
filter() Select elements based on a condition
reduce() Combine all elements into one
forEach() Perform action on each element
collect() Gather results into a collection
sorted() Sort elements
limit() Get first n elements
skip() Skip first n elements
distinct() Remove duplicates
flatMap() Flatten nested structures
anyMatch() Check if any element matches
findFirst() Get first element (if any)
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.