How it works...

In this recipe you learnt how to use three methods to process all the elements of a stream and apply an action to them. These methods are:

  • forEach(): This is a terminal operation that applies an action to all the elements of Stream and returns a void value. It receives as parameter the action to apply to the elements defined as a lambda expression or as an implementation of the Consumer interface. There's no guarantee about the order in which the action will be applied to the elements of a parallel stream.
  • forEachOrdered(): This is a terminal operation that applies an action to all the elements of Stream in the order of the stream, if the stream is an ordered stream, and returns a void value. You can use this method after ...

Get Java 9 Concurrency Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.