Do you want to iterate through java.util.Map and java.util.List in Java 8 using latest JDK8? I recently came across the same while working on my production application.
Topic looks very simple but it’s worth trying and adopting
new ways to iterate through Map and List. Small tips and possible Interview Question you may get in the future.
Sometime back I have written an article which covers few methods only for List. But in this tutorial we will go over all possible ways you could iterate for Map and List both.
Let’s get started:
- Create class CrunchifyJava8ForEachTutorialMapAndList.java
- Create Map called crunchifyCompanyMap
- Iterate through above map in two different ways
- standard method
- java8 way
- Create List called crunchifyCompanyList
- Iterate through above list in three different ways
- standard method
- java 8 – lambda method
- java 8 – method reference
Here is a complete Java Example
package com.crunchify.tutorials; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class CrunchifyJava8ForEachTutorialMapAndList { public static void main(String[] args) { // =============== MAP ================ Map<String, String> crunchifyCompanyMap = new HashMap<>(); crunchifyCompanyMap.put("Google", "Mountain View"); crunchifyCompanyMap.put("Facebook", "Santa Clara"); crunchifyCompanyMap.put("Twitter", "San Francisco"); // Method1: Standard Method to iterate through Java Map CrunchifyStandardForEachMethod4Map(crunchifyCompanyMap); // Method2: Java8 Method to iterate through Java Map CrunchifyJava8ForEachMethod4Map(crunchifyCompanyMap); // =============== List ================ List<String> crunchifyCompanyList = new ArrayList<>(); crunchifyCompanyList.add("Google"); crunchifyCompanyList.add("Facebook"); crunchifyCompanyList.add("Twitter"); // Method3: Standard Method to iterate through Java List CrunchifyStandardForEachMethod4List(crunchifyCompanyList); // Method4,5: Java8 Method to iterate through Java List CrunchifyJava8ForEachMethod4List(crunchifyCompanyList); } private static void CrunchifyStandardForEachMethod4Map( Map<String, String> crunchifyCompanyMap) { log("============ Method1: Standard Method to iterate through Java Map"); for (Map.Entry<String, String> crunchifyEntry : crunchifyCompanyMap.entrySet()) { log("crunchifyCompany: " + crunchifyEntry.getKey() + ", address: " + crunchifyEntry.getValue()); } } private static void CrunchifyJava8ForEachMethod4Map(Map<String, String> crunchifyCompanyMap) { log("\n============ Method2: Java8 Method to iterate through Java Map"); crunchifyCompanyMap.forEach((k, v) -> log("crunchifyCompany: " + k + ", address: " + v)); } private static void CrunchifyStandardForEachMethod4List(List<String> crunchifyList) { log("\n============ Method3: Standard Method to iterate through Java List"); for (String item : crunchifyList) { log(item); } } private static void CrunchifyJava8ForEachMethod4List(List<String> crunchifyList) { // lambda method log("\n============ Method4: Java8 Method to iterate through Java List - using Lambda"); crunchifyList.forEach(item -> log(item)); // using method reference log("\n============ Method5: Java8 Method to iterate through Java List - using Method Reference"); crunchifyList.forEach(System.out::println); } // Simple log utility private static void log(String string) { System.out.println(string); } }
Output:
============ Method1: Standard Method to iterate through Java Map crunchifyCompany: Google, address: Mountain View crunchifyCompany: Twitter, address: San Francisco crunchifyCompany: Facebook, address: Santa Clara ============ Method2: Java8 Method to iterate through Java Map crunchifyCompany: Google, address: Mountain View crunchifyCompany: Twitter, address: San Francisco crunchifyCompany: Facebook, address: Santa Clara ============ Method3: Standard Method to iterate through Java List Google Facebook Twitter ============ Method4: Java8 Method to iterate through Java List - using Lambda Google Facebook Twitter ============ Method5: Java8 Method to iterate through Java List - using Method Reference Google Facebook Twitter