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:

package org.arpit.java2blog;
import java.util.HashMap;

public class HashMapIterationMap {

 public static void main(String args[])
 {
  // HashMap with Country as key and capital as value
  HashMap<String,String> countryCapitalMap=new HashMap<String,String>();
  countryCapitalMap.put("India","Delhi");
  countryCapitalMap.put("Japan","Tokyo");
  countryCapitalMap.put("France","Paris");
  countryCapitalMap.put("Russia","Moscow");

  // Iterating Using keySet() and for each loop
  System.out.println("Iterating Using keySet() and for each loop");
  for (String countryKey:countryCapitalMap.keySet()) {
   System.out.println("Country:"+ countryKey +" and  Capital:"+countryCapitalMap.get(countryKey));
  }
  System.out.println("-----------------------------");
 }

}

Improved way of iterating HashMap in java 8:

package org.arpit.java2blog;
import java.util.HashMap;

public class HashMapIterationMap {

 public static void main(String args[])
 {
  // HashMap with Country as key and capital as value
  HashMap<String,String> countryCapitalMap=new HashMap<String,String>();
  countryCapitalMap.put("India","Delhi");
  countryCapitalMap.put("Japan","Tokyo");
  countryCapitalMap.put("France","Paris");
  countryCapitalMap.put("Russia","Moscow");

 
  // Iterating Using Java 8
  System.out.println("Iterating Using Java 8");
  System.out.println("-----------------------------");
  
  countryCapitalMap.forEach((k,v)->System.out.println("Country:" + k + " Capital : " + v));
  
  System.out.println("-----------------------------");
 }

}
When you run above program, you will get below output:
Iterating Using Java 8
-----------------------------
Country:Japan Capital : Tokyo
Country:France Capital : Paris
Country:India Capital : Delhi
Country:Russia Capital : Moscow
-----------------------------

List:

Normal way of iterating list:

package org.arpit.java2blog;
import java.util.ArrayList;

public class IteratingArrayListMain {

 public static void main(String[] args) {
   ArrayList<String> nameList=new ArrayList<String>();//creating arraylist  
   nameList.add("Arpit");//adding object in arraylist  
   nameList.add("John");  
   nameList.add("Martin");  
   nameList.add("Adam"); 
   // Iterating list using foreach loop
    for(String name :nameList )
    {
     System.out.print(name+" ");
    }
 }
}

Iterating through list in java 8:

package org.arpit.java2blog;
import java.util.ArrayList;

public class IteratingArrayListMain {

 public static void main(String[] args) {
   ArrayList<String> nameList=new ArrayList<String>();//creating arraylist  
   nameList.add("Arpit");//adding object in arraylist  
   nameList.add("John");  
   nameList.add("Martin");  
   nameList.add("Adam"); 
   // Iterating list using foreach method
   nameList.forEach(n->System.out.print(n+" "));
 }
}
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:
Arpit John Martin Adam 

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus

 

Java tutorial for beginners Copyright © 2012