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