Map Interface Example



A Map

- cannot contain duplicate keys

- each key can map to at most one value


The following example explains how to iterate through a Map in various Java versions including Java 8.


			
			


package com.smoothexample.java.set;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MapExample {
	public static void main(String[] args) {
		Map<String, String> map = new HashMap<String, String>();

		populateValues(map);

		System.out.println("----------------------------------------------------------------------");
		System.out.println("Using Map.Entry");
		System.out.println("----------------------------------------------------------------------");

		Set<Map.Entry<String, String>> entrySet = map.entrySet();
		for (Map.Entry<String, String> mapEntry : entrySet) {
			System.out.print(" key = " + mapEntry.getKey());
			System.out.println(" value = " + mapEntry.getValue());
		}

		System.out.println("----------------------------------------------------------------------");
		System.out.println("Using iterating through key, using Set");
		System.out.println("----------------------------------------------------------------------");
		Set<String> keys = map.keySet();
		for (String key : keys) {
			System.out.print("key = " + key);
			System.out.println("value = " + map.get(key));
		}

		System.out.println("----------------------------------------------------------------------");
		System.out.println("Using Java 8");
		System.out.println("----------------------------------------------------------------------");

		map.forEach((key, value) ->
			System.out.println("key = " + key + "  value = " + value));

		System.out.println("----------------------------------------------------------------------");
		System.out.println("Using Java 8, multiple statements in forEach");
		System.out.println("----------------------------------------------------------------------");

		map.forEach((key, value) -> {
			System.out.print("key = " + key);
			System.out.println("  value = " + value);
		});

	}

	private static void populateValues(Map<String, String> map) {
		for (int i = 1; i <= 10; i++) {
			String s = "name" + i;
			map.put(String.valueOf(i), s);
		}
	}

}





									


Sample output

             	
---------------------------------------------------------------------- Using Map.Entry ---------------------------------------------------------------------- key = 1 value = name1 key = 2 value = name2 key = 3 value = name3 key = 4 value = name4 key = 5 value = name5 key = 6 value = name6 key = 7 value = name7 key = 8 value = name8 key = 9 value = name9 key = 10 value = name10 ---------------------------------------------------------------------- Using iterating through key, using Set ---------------------------------------------------------------------- key = 1value = name1 key = 2value = name2 key = 3value = name3 key = 4value = name4 key = 5value = name5 key = 6value = name6 key = 7value = name7 key = 8value = name8 key = 9value = name9 key = 10value = name10 ---------------------------------------------------------------------- Using Java 8 ---------------------------------------------------------------------- key = 1 value = name1 key = 2 value = name2 key = 3 value = name3 key = 4 value = name4 key = 5 value = name5 key = 6 value = name6 key = 7 value = name7 key = 8 value = name8 key = 9 value = name9 key = 10 value = name10 ---------------------------------------------------------------------- Using Java 8, multiple statements in forEach ---------------------------------------------------------------------- key = 1 value = name1 key = 2 value = name2 key = 3 value = name3 key = 4 value = name4 key = 5 value = name5 key = 6 value = name6 key = 7 value = name7 key = 8 value = name8 key = 9 value = name9 key = 10 value = name10