Introduction
In the second part, we will look at terminal operations. Terminal operations close the stream and are always at the end of the stream. The most often used is the collect() operation to collect all elements of a collection.
1. Collect
Definition: Collects the elements of the stream into a collection or a map.
In our example, we want to have the names of different Persons in our own list
2. Count
Definition: Returns the number of elements in the stream.
In our example, we just want to count the list length. Therefore we can use count.
As you can see, since we do some operations like map() and filter() before, we do not know the size of the result list. (Maybe you can in this simple example…). But when you only need to know the size of the result list, count() is a great method.
3. AnyMatch
Definition: Returns true if any element of the stream matches a given predicate.
With anyMatch() you can quickly check if a list matches a given predicate. In our case we check that the List contains a Person with the name „Dennis“
4. AllMatch
Definition: Returns true if all elements of the stream match a given predicate.
With allMatch() you can quickly check if every list element matches a given predicate. In our case, we check if every person has the Name „Dennis“, which will result to false
List of terminal operations
There are many more terminal operations. Here is a short list of many of them.
- forEach(): Performs an action for each element of the stream.
- toArray(): Returns an array containing the elements of the stream.
- reduce(): Reduces the elements of the stream to a single value by applying a binary operator.
- noneMatch(): Returns true if no element of the stream matches a given predicate.
- findFirst(): Returns the first element of the stream, or an empty Optional if the stream is empty.
- findAny(): Returns any element of the stream, or an empty Optional if the stream is empty.
- min(): Returns the minimum element of the stream, according to a given comparator.
- max(): Returns the maximum element of the stream, according to a given comparator.
- sum(): Returns the sum of the elements in the stream.
- average(): Returns the average of the elements in the stream.
- partitioningBy(): Returns a Map> that partitions the elements of the stream into two groups based on a given predicate.
- groupingBy(): Returns a Map> that groups the elements of the stream by a given classification function.
- joining(): Returns a String that concatenates the elements of the stream, separated by a delimiter.
- forEachOrdered(): Performs an action for each element of the stream, in encounter order.
- toArray(IntFunction generator): Returns an array containing the elements of the stream, using the specified generator function to create the array.
- reduce(identity, accumulator): Reduces the elements of the stream to a single value using an identity value and an accumulator function.
- reduce(accumulator): Reduces the elements of the stream to a single value using an accumulator function.
- parallel(): Returns a parallel stream of the same elements as the current stream.
Groupingby multiple fields – Custom implementation
Now we want to implement our own implementation.
If we want to group data, by given criteria, we can use groupingBy(). But this only applies to one criteria. But maybe we want to group them by multiple criteria.
In our case, we want to group people with the same name and age together. Therefore we implement our own groupingBy.
Conclusion
Here you saw many different kinds of Java-Streams. In the next part, we will talk about parallel streams. You should not miss that 🙂
Pingback: Java Stream Intermediate Operations with examples – Part 1/2 – Code Nest