This example will show various code snippets to demonstrate stream terminal operations. Terminal operations produces a non-stream, result such as primitive value, a collection or no value at all. Terminal operations are typically preceded by intermediate operations which return another Stream which allows operations to be connected in a form of a query.
The Stream.forEach method will perform an action for each element in the stream. It is a simplified inline way to write a for loop. Below we will iterate over each element and will call System.our.println.
Stream.reduce operations are commonly found in statistic operations such as long summary statistics and combine the stream elements into one using a BinaryOperator. In this snippet, we will find the sum of all elements in a stream.
The Stream.collect terminal operation will convert the stream into some other container such as a list. In the snippet below, we will convert a stream to a set. Other uses cases for the Stream.collect can be seen in convert a collection to a map, filter map by entries, stream group by, and joining strings.
Finding the minimum of a stream is another statistics type operation which can be performed by calling Stream.min. In this snippet we create an IntStream which is a specialized stream for handling primitive int and call the min. Finding the minimum of a stream is similar to finding the minimum of an array or minimum of a list.
Stream.max will find the maximum element of the stream according to a specified comparator. In the snippet below we will call Stream.mapToDouble which apply the given function Double::doubleValue to each element returning a DoubleStream. Finding the max element in a stream is similar to finding max value in a list and max value in an array.
Stream.count will find the number of elements in the stream. Counting the number of elements in a stream has similarities to counting the occurrences in a list, counting non empty strings in a collection and count words in file.
Stream.anyMatch will find out whether at least one of the elements in the stream matches a given predicate. In this snippet, we will create a predicate from a lambda expression to check if the length of a string is greater than 5. Comparable examples can seen in guava iterables example and list contains any element.
Who doesn't like cookies! Stream.allMatch will check every element in the stream and find out if it matches the predicate. In the snippet below we will create a predicate with a lambda expression to check that each element in the list contains the character sequence "Cookies". Other related examples include list contains all and guava iterables all.
Just the opposite of Stream.anymatch, Stream.noneMatch will find if no elements in the stream match the specified predicate. In the snippet below, we will create a IntStream and then check each of the elements doesn't equal to 5. In addition to this snippet, the stream find and match example has a neat way of showing the noneMatch in relation to a hidden game object.
Stream.findFirst will find the first element in the stream which is resembles the same behavior as getting the first element in a list.
Similar to finding any element in array, Stream.findAny will find any element in a given stream.