Java Stream Terminal Operations – Part 2/2

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

Java

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.

Java

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“

Java

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

Java

List of terminal operations

There are many more terminal operations. Here is a short list of many of them.

  1. forEach(): Performs an action for each element of the stream.
  2. toArray(): Returns an array containing the elements of the stream.
  3. reduce(): Reduces the elements of the stream to a single value by applying a binary operator.
  4. noneMatch(): Returns true if no element of the stream matches a given predicate.
  5. findFirst(): Returns the first element of the stream, or an empty Optional if the stream is empty.
  6. findAny(): Returns any element of the stream, or an empty Optional if the stream is empty.
  7. min(): Returns the minimum element of the stream, according to a given comparator.
  8. max(): Returns the maximum element of the stream, according to a given comparator.
  9. sum(): Returns the sum of the elements in the stream.
  10. average(): Returns the average of the elements in the stream.
  11. partitioningBy(): Returns a Map> that partitions the elements of the stream into two groups based on a given predicate.
  12. groupingBy(): Returns a Map> that groups the elements of the stream by a given classification function.
  13. joining(): Returns a String that concatenates the elements of the stream, separated by a delimiter.
  14. forEachOrdered(): Performs an action for each element of the stream, in encounter order.
  15. toArray(IntFunction generator): Returns an array containing the elements of the stream, using the specified generator function to create the array.
  16. reduce(identity, accumulator): Reduces the elements of the stream to a single value using an identity value and an accumulator function.
  17. reduce(accumulator): Reduces the elements of the stream to a single value using an accumulator function.
  18. 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.

Java

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 🙂

1 Kommentar zu „Java Stream Terminal Operations – Part 2/2“

  1. Pingback: Java Stream Intermediate Operations with examples – Part 1/2 – Code Nest

Kommentar verfassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Nach oben scrollen