Table of Contents
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
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("-----------------------------"); } } |
Iterating Using Java 8
—————————–
Country:Japan Capital : Tokyo
Country:France Capital : Paris
Country:India Capital : Delhi
Country:Russia Capital : Moscow
—————————–
—————————–
Country:Japan Capital : Tokyo
Country:France Capital : Paris
Country:India Capital : Delhi
Country:Russia Capital : Moscow
—————————–
List:
Normal way of iterating list:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package org.arpit.java2blog; import java.util.ArrayList; public class IteratingArrayListMain { public static void main(String[] args) { ArrayList nameList=new ArrayList();//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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; import java.util.ArrayList; public class IteratingArrayListMain { public static void main(String[] args) { ArrayList nameList=new ArrayList();//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+" ")); } } |
Arpit John Martin Adam
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.