“This is the 14th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Terminal operation

Terminal operations on Java Stream interfaces typically return a single value. Once an endpoint operation is invoked on a Stream, iterations of the Stream and any chain streams begin. When the iteration is complete, the result of the terminal operation is returned.

Terminal operations typically do not return a new Stream instance. Therefore, once you invoke terminal operations on a Stream, linking the Stream instance from a non-terminal operation ends. Here is an example of calling a terminal operation on a Java Stream:

long count = stream .map((value) -> { return value.toLowerCase(); }) .map((value) -> { return value.toUpperCase(); }).map((value) -> {return value.substring(0,3); }) .count();Copy the code

The call to count() at the end of the example is a terminal operation. Since count() returns a long, the Stream chain (map() call) for non-terminal operations terminates.

anyMatch()

The Java Stream anyMatch() method is a terminal operation that takes singlePredicate as an argument, starts an internal iteration of the Stream, and applies the Predicate argument to each element. If Predicate returns true for any element, the anyMatch() method returns true. AnyMatch () returns false if there are no element match predicates. Here is an example of Java Stream anyMatch() :

In the example above, the anyMatch() method call will return true because the first string element in the stream begins with “One”.

allMatch()

The Java Stream allMatch() method is a terminal operation that takes singlePredicate as an argument, starts an internal iteration of the elements in the Stream, and applies the Predicate argument to each element. If Predicate returns true for all elements in the stream, then allMatch() will return true. The allMatch() method returns false if not all elements match Predicate. Here is an example of a Java stream ‘allMatch() :

In the example above, the allMatch() method will return false because there is only One string in the Stream beginning with “One”.

noneMatch()

The Java Stream noneMatch() method is an endpoint operation that iterates over elements in the Stream and returns true or false, depending on whether there are no elements in the Stream that match the predicate passed to noneMatch(). The noneMatch() method returns true if Predicate does not match elements, false if it matches one or more elements. Here is an example of Java Stream noneMatch() :

collect()

The Java Stream Collect () method is a terminal operation that starts an internal iteration of elements and collects elements in the Stream as a collection or object of some type. Here is an example of a simple Java Stream collect() method:

Collect () method to the Collector (Java. Util. Stream. The Collector) as a parameter. Implementing the Collector requires some research into the Collector interface. Fortunately, the Java classes in Java. Util. Stream. The Collectors contains a set can be used for the operation of the most common of the Collector. In the example above, the Collector implementation returned by Collectors. ToList () is used. This Collector simply collects the List of all elements in the flow in standard Java

count()

The Java Stream Count () method is an endpoint operation that begins an internal iteration of the elements in the Stream and counts the elements. Here is an example of a Java stream ‘count() :

This example starts by creating a string List, gets the Stream of that List, adds the aflatMapflatMap() operation to it, and ends with a call to count(). The count() method starts the iteration of the elements in the Stream, which causes the string elements to be broken up into words in the flatMap() operation and then counted. The final printed result is 14.

findAny()

The Java Stream findAny() method finds individual elements from the Stream. The elements found can come from anywhere in the stream. There is no guarantee of where elements are fetched from in the flow. Here is an example of a Java Stream findAny() :

Notice how the findAny() method returns an Optional. Streams can be empty — and therefore cannot return any elements. You can check to see if an element is found by using the optional isPresent() method.

findFirst()

If there are any elements in the Stream, the Java Stream findFirst() method looks for the first element in the Stream. The findFirst() method returns an optional element from which you can get the element, if one exists. Here is an example of a Java Stream findFirst() :\

You can check whether the Optional returned contains elements by using the isPresent() method.

forEach()

The Java Stream forEach() method is an endpoint operation that begins an internal iteration of elements in the Stream and applies a Consumer(java.util.function.consumer) to each element in the Stream. The forEach() method returns void. Here is a Java Stream forEach() example:

min()

The Java Stream min() method is an endpoint operation that returns the smallest element in the Stream. Which element is the smallest is determined by the Comparatorimplementation passed to the min() method. I’ve explained how the Comparatorinterface works in the tutorial on Java collection sorting. Here is an example of a Java stream ‘ ‘min() :

Note how the min() method returns an Optional, which may or may not contain the result. If the Stream is empty, the optional get() method will throw NoSuchElementException.

max()

The Java Stream Max () method is an endpoint operation that returns the largest element in the Stream. Which element is the largest is determined by the Comparatorimplementation passed to the Max () method. I’ve explained how the Comparatorinterface works in the tutorial on Java collection sorting. Here is an example of a Java stream “Max () :

Note how the Max () method returns an Optional, which may or may not contain the result. If the Stream is empty, the optional get() method will throw NoSuchElementException.

reduce()

The Java Stream Reduce () method is an endpoint operation that reduces all elements in a Stream to a single element. Here is an example of Java Stream Reduce () :

Note the Optional return from the reduce() method. This Optional contains the value (if any) returned by the lambda expression passed to the reduce() method. You can get this value by calling the Optional get() method.

toArray()

The Java Stream toArray() method is a terminal operation that starts an internal iteration of the elements in the Stream and returns an Object array containing all the elements. Here is an example of a Java Stream toArray() :