Java_Stream_API_30_Functions_Explained
Java_Stream_API_30_Functions_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());
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);
Example:
nums.stream().anyMatch(n -> n > 5);
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()));
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);
Example:
nums.stream().mapToInt(Integer::intValue).sum();
23. collectingAndThen()
Applies finishing transformation after collecting.
Example:
list.stream().collect(Collectors.collectingAndThen(Collectors.toList(),
Collections::unmodifiableList));
Example:
nums.stream().reduce((a, b) -> a * b);
25. toCollection()
Collects into specific collection type.
Example:
names.stream().collect(Collectors.toCollection(LinkedList::new));
Example:
names.stream().toArray(String[]::new);
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());
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);