Using Lambda Expression to foreach a List object in Java

In this tutorial, I will show you all how using Lambda Expression to foreach a List object in Java.

OK, let’s get started!

Usually from Java 7 and earlier, whenever you want to print out elements in a List object, for example:

We usually code as follows:

or:

Since Java 8, we have several other ways to do this: using the forEach () method in the List object with the Lambda Expression, detail as below:

or with Method reference:

The argument of forEach () method in the List object is the Consumer, a Functional Interface.

Result:

Chia sẽ bài viết này ...Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment