Java 8 forEach examples

In this post, we will see improved way of iterating through map and list which are introduced in java 8.

Map:

Normal way of iterating HashMap before java 8:

Improved way of iterating HashMap in java 8:

When you run above program, you will get below output:

List:

Normal way of iterating list:

Iterating through list in java 8:

forEach method internally use Consumer Object.You can read more about Java 8 Consumer interface.
When you run above program, you will get below output:

Add Comment