Stream forEach(Consumer action) performs an action for each element of the stream. Stream forEach(Consumer action) is a terminal operation i.e, it may traverse the stream to produce a result or a side-effect.
void forEach(Consumer<? super T> action) Where, Consumer is a functional interface and T is the type of stream elements.
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 : To perform print operation on each element of reversely sorted stream.
10 8 6 4 2
Example 2 : To perform print operation on each element of string stream.
GFG Geeks for GeeksforGeeks
Example 3 : To perform print operation on each element of reversely sorted string stream.
o e e F
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.