LongStream forEach() method in Java
LongStream forEach(LongConsumer action) performs an action for each element of the stream. LongStream forEach(LongConsumer action) is a terminal operation i.e, it may traverse the stream to produce a result or a side-effect.
void forEach(LongConsumer action)
Parameter : LongConsumer represents an operation that accepts a single long-valued argument and returns no result. This is the primitive type specialization of Consumer for long.
Note : The behavior of this operation is explicitly nondeterministic. Also, for any given element, the action may be performed at whatever time and in whatever thread the library chooses.
Example 1 :
7 8 9 10
Example 2 :
4 5 6 7 8
Note : For parallel stream, LongStream forEach(LongConsumer action) does not guarantee to respect the encounter order of the stream, as doing so would sacrifice the benefit of parallelism. Below is the example.
Example 3 :
6 8 7 5 4
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.