×
☰ See All Chapters

Java forEach loop

forEach method is added to Iterable interface and can be used by all the collection which implement Iterable interface. This method is a default method. (default methods detailed in later chapters). The forEach method signature is as given below:

default void forEach(Consumer<? super T> action) {

        Objects.requireNonNull(action);

        for (T t : this) {

            action.accept(t);

        }

}

forEach method returns null and takes the Consumer object as an argument. forEach method performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.

The Consumer functional interface code is as given below:

@FunctionalInterface

public interface Consumer<T> {

 

    /**

     * Performs this operation on the given argument.

     *

     * @param t the input argument

     */

    void accept(T t);

 

    /**

     * Returns a composed {@code Consumer} that performs, in sequence, this

     * operation followed by the {@code after} operation. If performing either

     * operation throws an exception, it is relayed to the caller of the

     * composed operation.  If performing this operation throws an exception,

     * the {@code after} operation will not be performed.

     *

     * @param after the operation to perform after this operation

     * @return a composed {@code Consumer} that performs in sequence this

     * operation followed by the {@code after} operation

     * @throws NullPointerException if {@code after} is null

     */

    default Consumer<T> andThen(Consumer<? super T> after) {

        Objects.requireNonNull(after);

        return (T t) -> { accept(t); after.accept(t); };

    }

}

 

forEach example

package com.java4coding;

 

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

public class Demo {

 

        public static void main(String args[]) {

                Map<String, Integer> items = new HashMap<>();

                items.put("Manu", 1);

                items.put("Advith", 2);

                items.put("Tyagraj", 3);

                items.put("Likitha", 4);

 

                items.forEach((name,id)->System.out.println("Name : " + name + " ID : " + id));

               

                items.forEach((name,id)->{

                        System.out.println("Name : " + name + " ID : " + id);

                        if("Manu".equals(name)){

                                System.out.println("Hello " + name);

                        }

                });

               

                System.out.println("*****************************");

               

                List<String> names = new ArrayList<>();

                names.add("Manu");

                names.add("Advith");

                names.add("Tyagraj");

                names.add("Likitha");

 

                names.forEach(name->System.out.println(name));

                       

                names.forEach(name->{

                        if("Manu".equals(name)){

                                System.out.println(name);

                        }

                });

        }

}

Output:

Name : Advith ID : 2

Name : Likitha ID : 4

Name : Tyagraj ID : 3

Name : Manu ID : 1

Name : Advith ID : 2

Name : Likitha ID : 4

Name : Tyagraj ID : 3

Name : Manu ID : 1

Hello Manu

*****************************

Manu

Advith

Tyagraj

Likitha

Manu

 


All Chapters
Author