š§ Core Stream Functions in Java Lambda Processing
1. š¹ map(Function<T, R>)
Purpose: Transforms each element into another form (1-to-1 mapping)
Use Case: Convert objects, extract fields, modify values
2. š¹ filter(Predicate<T>)
Purpose: Filters elements that match a condition
Use Case: Keep only items that satisfy a condition
3. š¹ reduce(BinaryOperator<T>)
Purpose: Combines all stream elements into a single result
Use Case: Calculate sum, product, max, concatenate, etc.
4. š¹ forEach(Consumer<T>)
Purpose: Performs an action for each element (terminal operation)
Use Case: Printing, updating external variables (use with caution in parallel streams)
5. š¹ collect(Collector)
Purpose: Converts stream to a collection or summarizing result
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
Use Case: Sorting by value, property, custom logic
7. š¹ limit(n)
& skip(n)
Purpose: Truncate or skip elements from the stream
Use Case: Pagination, trimming input
8. š¹ distinct()
Purpose: Removes duplicate elements (based on equals()
)
Use Case: Ensure uniqueness in a collection
9. š¹ flatMap(Function<T, Stream<R>>)
Purpose: Flattens nested streams into a single stream
Use Case: Flatten nested lists, transform collections of collections
10. š¹ anyMatch
, allMatch
, noneMatch
Purpose: Checks for match conditions
Use Case: Validation, existence checks
11. š¹ findFirst()
, findAny()
Purpose: Get the first or any available element (wrapped in Optional)
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) |