Java Stream API - 30 Methods Explained
with Examples
1. Creating a Stream
Create a stream from a collection like List or Set.
Example:
List<Integer> nums = [Link](1, 2, 3);
Stream<Integer> stream = [Link]();
2. forEach()
Performs an action for each element in the stream.
Example:
[Link]().forEach(n -> [Link](n));
3. filter()
Filters elements based on a condition.
Example:
[Link]().filter(n -> n % 2 == 0).collect([Link]());
4. map()
Transforms elements of the stream.
Example:
[Link]().map(n -> n * n).collect([Link]());
5. collect()
Collects results into a collection like List, Set, etc.
Example:
[Link]().collect([Link]());
6. sorted()
Sorts elements in natural or custom order.
Example:
[Link]().sorted().collect([Link]());
7. distinct()
Removes duplicates from the stream.
Example:
[Link]().distinct().collect([Link]());
8. limit() and skip()
limit(n): returns first n elements; skip(n): skips first n elements.
Example:
[Link]().limit(2).collect([Link]());
9. reduce()
Combines elements using a binary operation.
Example:
[Link]().reduce(0, (a, b) -> a + b);
10. anyMatch(), allMatch(), noneMatch()
Checks if any, all, or none of the elements match a condition.
Example:
[Link]().anyMatch(n -> n > 5);
11. findFirst() and findAny()
Returns the first or any element wrapped in Optional.
Example:
[Link]().findFirst();
12. parallelStream()
Processes stream in parallel using multiple threads.
Example:
[Link]().forEach([Link]::println);
13. groupingBy()
Groups elements based on a classifier function.
Example:
[Link]().collect([Link](name -> [Link](0)));
14. partitioningBy()
Partitions elements into two groups based on a predicate.
Example:
[Link]().collect([Link](n -> n % 2 == 0));
15. joining()
Joins strings in the stream.
Example:
[Link]().collect([Link](", "));
16. peek()
Performs an action on each element and returns the stream.
Example:
[Link]().peek([Link]::println).map(String::toUpperCase);
17. toMap()
Collects elements into a Map.
Example:
[Link]().collect([Link](name -> name, name -> [Link]()));
18. Custom Comparator with sorted()
Sorts elements using custom comparator.
Example:
[Link]().sorted([Link](String::length));
19. flatMap()
Flattens a stream of collections into a single stream.
Example:
[Link]().flatMap(List::stream).collect([Link]());
20. count()
Returns the count of elements in the stream.
Example:
[Link]().count();
21. max() and min()
Finds maximum or minimum element.
Example:
[Link]().max(Integer::compare);
22. mapToInt() / mapToDouble()
Converts stream to primitive stream for numerical operations.
Example:
[Link]().mapToInt(Integer::intValue).sum();
23. collectingAndThen()
Applies finishing transformation after collecting.
Example:
[Link]().collect([Link]([Link](),
Collections::unmodifiableList));
24. reduce() with no initial value
Returns an Optional with reduced result.
Example:
[Link]().reduce((a, b) -> a * b);
25. toCollection()
Collects into specific collection type.
Example:
[Link]().collect([Link](LinkedList::new));
26. Stream to Array
Converts stream to array.
Example:
[Link]().toArray(String[]::new);
27. toMap() with conflict resolution
Handles key conflicts by merging values.
Example:
[Link]().collect([Link](w -> [Link](0), w -> w, (a, b) -> a + "," + b));
28. Filter & Map complex objects
Works with object streams.
Example:
[Link]().filter(u -> [Link] > 18).map(u -> [Link]).collect([Link]());
29. takeWhile() and dropWhile() (Java 9+)
takeWhile: stops when predicate fails, dropWhile: skips until predicate fails.
Example:
[Link]().takeWhile(n -> n < 4);
30. [Link]()
Creates an infinite stream of elements.
Example:
[Link](() -> "Hello").limit(3).forEach([Link]::println);