forEach() Method on Iterable Interface Example in Java 8

We also have new forEach() method  on Iterable Interface, so it will be available to all implementation of Collection’s since they all themselves are Iterable. Unfortunately this method is not for arrays.

forEach() method implementation is very simple. It takes a Consumer Functional Interface and this Consumer will be executed for each element of the Iterable. Let suppose I want to print down all the lawn tennis tournaments of a list.

I just called forEach method on tenisTournamnetList and call System.out::println, this is method reference, which is strictly equivalent to lambda expressions. That lambda expression takes out an String and System.out::println  print given stream and the output of the programme is:


Leave a Reply

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