Using Lambda Expression to foreach a Map object in Java

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

OK, let’s get started!

Same like List object, the forEach () method is also added to the Map object since Java 8, which gives us more ways to print the elements in this object.
For example, I have a Map object like this:

Normally, I would write code like this:

With the forEach() method and Lambda Expression, our code will be as simple as this:

The argument of forEach() method in the Map object is BiConsumer, a Functional Interface.
Result:

Add Comment