Quantcast
Channel: Crunchify
Viewing all articles
Browse latest Browse all 1037

In Java 8 How to Iterate Through java.util.Map and java.util.List? Example attached with Total 5 Different Ways

$
0
0

Iterate through Map and List in Java8 - Crunchify Tutorial

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:

  1. Create class CrunchifyJava8ForEachTutorialMapAndList.java
  2. Create Map called crunchifyCompanyMap
  3. Iterate through above map in two different ways
    1. standard method
    2. java8 way
  4. Create List called crunchifyCompanyList
  5. Iterate through above list in three different ways
    1. standard method
    2. java 8 – lambda method
    3. 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

Have a suggestion on article? Please chime in and share it as a comment.

The post In Java 8 How to Iterate Through java.util.Map and java.util.List? Example attached with Total 5 Different Ways appeared first on Crunchify.

Author: App Shah

Crunchify, LLC Logo


Viewing all articles
Browse latest Browse all 1037

Trending Articles