forEach Expression

Starting Java 8, a new method forEach is introduced to iterate the elements.

This forEach(Consumer<? super T> action) method is created in the following interfaces in java.

  • java.lang.Iterable
  • java.util.Stream

This method performs a user given action on each element.

In the below example, we can iterate a list of collections with forEach defining a condition.

 

Result:

Leave a Reply

Your email address will not be published. Required fields are marked *