Java 8 New FeaturesJava Interview Questions

Java 8 forEach loop explained with code samples

Posted On
Posted By admin

Java 8 introduced a lot of new features like functional interfaces, lambda expressions, etc. These provide a totally new way of programming for developers. One such feature is the forEach support added to the Collection interfaces. In this blog post, I will be covering this feature in detail.

What is forEach

Java 8 added a new method called forEach to all the Collection interfaces via the Iterable interface. You can use this method to iterate through a Collection internally without the need for an explicit for loop.

Before Java 8

Prior to Java 8, if we wanted to iterate through a Collection like a List, we had to write code similar to the following:


public class ForEachDemo {
public static void main(String args[]){
List <Integer> list = Arrays.asList(5,3,11,15,9);
for(int num:list){
System.out.println("Number is "+num);
}

}
}

So you need to write a for loop to iterate through the elements in the List. In the body of the for loop, you can write the code that you want to be executed on each element in the list.

Java 8 Way

Starting with Java 8, you can iterate through a Collection using the forEach method.  So you can write code similar to the following:


public class ForEachDemo {
public static void main(String args[]){
List<Integer> list = Arrays.asList(5,3,11,15,9);
list.forEach(num -> System.out.println("Number is "+num));

}
}

So here, we are invoking the forEach method on the List interface. We are passing a lambda expression to the forEach method that prints the current element.  So, you can see that there is no explicit for loop used here.

How forEach works

Java 8 has added a new method called forEach on the Iterable Interface.  The Collection interface extends this interface and so this method is automatically available in all the Collection interfaces like Set and List

The forEach method accepts a single argument which is a Consumer instance. Consumer is a functional interface. Refer to this blog post to understand more about functional interfaces. Consumer has a single method called accept which accepts input of any data type.

The forEach method is given a default implementation within the Iterable interface. Refer to this blog post to understand default interface methods introduced by Java 8. This is just to avoid making changes to the code for all the Collection classes to provide an implementation for this method. If the default implementation was not provided, Java would have to provide an implementation for the forEach method in each of the Collection classes like ArrayList, HashSet. etc.

The default implementation of the forEach method iterates through all the elements of the Collection on which it is invoked and invokes the accept method with the specified lambda expression. Here we are using a lambda expression that simply prints the input number. So the forEach method invokes the accept method with this expression and prints each number in the input list.

Just like List,  you can use the forEach method to iterate over a Map or Set as well.

Why forEach is better

Compared to the traditional approach, the Java 8 approach is much better. The traditional approach is verbose and requires explicit code to be written. In contrast, the Java 8 code is concise and easy to read.

If you'd like to watch a detailed video tutorial of this topic or other related topics, do check out my new course Learn Java 8 New Features

If you like this post, please do let me know via the comments box below.  You can also connect with me via my Facebook Page or subscribe to my Youtube channel!

Related Post

leave a Comment