Java 8 forEach method Example

7 years ago Lalit Bhagtani 0

In this tutorial, we will learn about new Java 8 forEach() method.

java 8 foreach

Map’s forEach() method :-

This method is called on map object, it takes BiConsumer as an argument and perform given action for each entry in the map until all entries have been processed or if action throws an exception.

Syntax :-  default void forEach(BiConsumer<? super K,? super V> action)

Reference :-  Map forEach() method JavaDocs 

Collection forEach() method :-

This method is called on collection object,it takes Consumer as an argument and perform given action for each element in the map until all entries have been processed or if action throws an exception.

Syntax :-  default void forEach(Consumer<? super T> action)

Reference :-  Collection forEach() method JavaDocs 

Problem Statement :-

You have given a collection (List / map / set) of names, iterate through the collection and print all the names.

Let’s see the implementation of above problem in java 7 and in java 8.

Java 7 :- forEach with List


  public static void main(String[] args){
    List<String> names = new ArrayList<String>();
    names.add("Robert");
    names.add("Martin");
    names.add("Jack");
    names.add("Anil");
 
    for(String name : names){
      System.out.println("Names : " + name);
    } 
 }

Java 8 :- forEach with List


  public static void main(String[] args){
    List<String> names = new ArrayList<String>();
    names.add("Robert");
    names.add("Martin");
    names.add("Jack");
    names.add("Anil");
 
    names.forEach( n -> System.out.println("Names : " + n));
 }

Result :- 

Names : Robert
Names : Martin
Names : Jack
Names : Anil

Java 7 :- forEach with Map


  public static void main(String[] args){
    Map<Integer,String> names = new HashMap<Integer,String>();
    names.put(1,"Robert");
    names.put(2,"Martin");
    names.put(3,"Jack");
    names.put(4,"Anil");
 
    Set<Entry<Integer,String>> entrySet = names.entrySet();
 
    for(Entry<Integer,String> nameEntry : entrySet){
      System.out.println("Names : " + nameEntry.getValue());
    } 
  }
 

Java 8 :- forEach with Map


  public static void main(String[] args){
    Map<Integer,String> names = new HashMap<Integer,String>();
    names.put(1,"Robert");
    names.put(2,"Martin");
    names.put(3,"Jack");
    names.put(4,"Anil");
 
    names.forEach( (i,s) -> System.out.println("Names : " + s)); 
 }

Result :- 

Names : Robert
Names : Martin
Names : Jack
Names : Anil

Java 7 :- forEach with Set


  public static void main(String[] args){
    Set<String> names = new HashSet<String>();
    names.add("Robert");
    names.add("Martin");
    names.add("Jack");
    names.add("Anil");
 
    for(String name : names){
      System.out.println("Names : " + name);
    } 
  }

Java 8 :- forEach with Set


  public static void main(String[] args){
    Set<String> names = new HashSet<String>();
    names.add("Robert");
    names.add("Martin");
    names.add("Jack");
    names.add("Anil");
 
    names.forEach( n -> System.out.println("Names : " + n));
 }

Result :- 

Names : Robert
Names : Martin
Names : Jack
Names : Anil

That’s all for Java 8 forEach method Example. If you liked it, please share your thoughts in comments section and share it with others too.