JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Java 8 - forEach method example with Map


forEach is a new method introduced in Java 8 to iterate over collections. Here is an example on forEach method to iterate over Map.

package com.java2novice.java8;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class ForEachMapEx {

	public static void main(String a[]) {

		Map<String, String> countryMap = new HashMap<>();
		countryMap.put("India", "Delhi");
		countryMap.put("USA", "Washington, D.C.");
		countryMap.put("Japan", "Tokyo");
		countryMap.put("Canada", "Ottawa");

		// iterate through Map normal way
		ForEachMapEx.iterateMap(countryMap);

		// iterate through Map using forEach method
		ForEachMapEx.iterateMapUsingForEach(countryMap);
	}

	public static void iterateMap(Map<String, String> countryMap) {

		System.out.println("<----------Iterating in normal way------------->");
		for(Entry<String, String> entry:countryMap.entrySet()) {
			System.out.println("Country: "+entry.getKey()+" : Capital: "+entry.getValue());
		}
	}

	public static void iterateMapUsingForEach(Map<String, String> countryMap) {

		System.out.println("\n<----------Iterating using forEach method------------>");
		countryMap.forEach((k,v)->System.out.println("Country: "+k+" : Capital: "+v));

		countryMap.forEach((k,v)->{
			// you can implement some business logic here..



		});
	}
}

Output:
<----------Iterating in normal way------------->
Country: Canada : Capital: Ottawa
Country: USA : Capital: Washington, D.C.
Country: Japan : Capital: Tokyo
Country: India : Capital: Delhi

<----------Iterating using forEach method------------>
Country: Canada : Capital: Ottawa
Country: USA : Capital: Washington, D.C.
Country: Japan : Capital: Tokyo
Country: India : Capital: Delhi
 Next Program >>

Java 8 forEach method examples

  1. Java 8 forEach example with Map
  2. Java 8 forEach example with List
Knowledge Centre
When to use LinkedList or ArrayList?
Accessing elements are faster with ArrayList, because it is index based. But accessing is difficult with LinkedList. It is slow access. This is to access any element, you need to navigate through the elements one by one. But insertion and deletion is much faster with LinkedList, because if you know the node, just change the pointers before or after nodes. Insertion and deletion is slow with ArrayList, this is because, during these operations ArrayList need to adjust the indexes according to deletion or insetion if you are performing on middle indexes. Means, an ArrayList having 10 elements, if you are inserting at index 5, then you need to shift the indexes above 5 to one more.
Famous Quotations
It’s not that I’m so smart, it’s just that I stay with problems longer.
-- Albert Einstein

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.