Java Streams Terminal Operations Explained with Examples
In our previous post, we explored intermediate operations in Java Streams. Now, let’s move to terminal operations. These are the final operations that produce a result (like a value, collection, or side-effect) and terminate the stream pipeline.
1. forEach()
The forEach() method is used to perform an action for each element of the stream.
List<String> words = Arrays.asList("apple", "banana", "cherry");
words.forEach(word -> System.out.println("item is " + word));
2. collect()
The collect() method gathers the elements of a stream into a collection (like a List, Set, or Map).
List<Integer> nums = Arrays.asList(1,1,2,2,2,3,4,5,5,5,6); Set<Integer> res = nums.stream().collect(Collectors.toSet()); System.out.println(res);
3. reduce()
The reduce() method performs a reduction on the elements of the stream using an accumulation function.
List<Integer> nums = Arrays.asList(1,2,3,4,5); Optional<Integer> res = nums.stream().reduce((a,b) -> a*b); System.out.println(res.get());
4. allMatch()
Checks if all elements of the stream satisfy a given condition.
List<Integer> nums = Arrays.asList(1,2,3,4,5); boolean res = nums.stream().allMatch(n -> n > 0); System.out.println(res);
5. anyMatch()
Checks if any element of the stream satisfies the condition.
List<Integer> nums = Arrays.asList(1,2,3,4,5); boolean res = nums.stream().anyMatch(n -> n > 4); System.out.println(res);
6. noneMatch()
Checks if no element in the stream satisfies the condition.
List<Integer> nums = Arrays.asList(1,2,3,4,5); boolean res = nums.stream().noneMatch(n -> n < 0); System.out.println(res);
7. findFirst()
Returns the first element of the stream wrapped in an Optional.
List<String> words = Arrays.asList("apple", "banana", "cherry");
Optional<String> res = words.stream().filter(w -> w.startsWith("c")).findFirst();
System.out.println(res.get());
8. findAny()
Returns any one element from the stream (especially useful in parallel streams).
List<String> words = Arrays.asList("apple", "banana", "cherry");
Optional<String> res = words.stream().filter(w -> w.startsWith("c")).findAny();
System.out.println(res.get());
9. max() and min()
Used to find the maximum and minimum elements using a comparator.
List<Integer> nums = Arrays.asList(9,5,2,7,3);
Optional<Integer> max = nums.stream().max((a, b) -> a-b);
Optional<Integer> min = nums.stream().min((a, b) -> a-b);
System.out.println("Max: " + max.get());
System.out.println("Min: " + min.get());
10. toArray()
Converts the stream into an array.
List<String> words = Arrays.asList("apple", "banana", "cherry");
String[] res = words.stream().toArray(String[]::new);
System.out.println(Arrays.toString(res));
11. count()
Counts the number of elements in the stream.
List<Integer> nums = Arrays.asList(1,2,3,4,5); long count = nums.stream().count(); System.out.println(count);
12. summaryStatistics()
For numerical streams, summaryStatistics() provides count, sum, min, average, and max in one go.
List<Integer> nums = Arrays.asList(1,2,3,4,5); IntSummaryStatistics stats = nums.stream().mapToInt(Integer::intValue).summaryStatistics(); System.out.println(stats);
Conclusion
Java Stream terminal operations are essential to extract results from a pipeline. From forEach() to summaryStatistics(), these operations help us transform, analyze, and finalize data in a concise way.
By combining intermediate and terminal operations, you can build powerful data-processing pipelines in Java with clean and readable code.
Comments
Post a Comment