Jun 152017

ADM

6 methods how to loop a Map in Java

  • 15 June 2017
  • ADM

 

 

 

6 methods how to loop a Map in Java - images/logos/java.jpg

 

In this article will describe how to loop through a Map object.

There are several ways to loop through a Map object, the main difference is if you need the keys:values or just the values.

  • method 1: is using entrySet method to get the key:value combination, then using an iterator it will loop through using a while loop.
  • method 2: is using keySet method to get the keys, then using a for loop will go through all the keys and will take the values from the map object.
  • method 3: is using entrySet method to get the key:value combination and will use a for loop to go through all key:value object.
  • method 4: is Java 8 specific and is using the forEach method from the map and the lambda to associate a method with it.
  • method 5: is using keySet to get the keys, then using a while loop will go through all the keys, the method is similar to method 2.
  • method 6: is using the values method to get a collection of values then using an iterator will loop through using while loop, also a for loop can be used to loop the iterator object.

Note that different combinations of these 6 methods can be used to loop through different types of collections and just changing the looping method you can get a different method.

Full example

package com.admfactory.basic;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class MapLoopExample {
    public static void main(String[] args) {
	System.out.println("Map loop example");
	System.out.println();

	/** creating a new Map object */
	Map<String, String> map = new HashMap<String, String>();

	/** adding some values */
	map.put("key1", "value1");
	map.put("key2", "value2");
	map.put("key3", "value3");
	map.put("key4", "value4");
	map.put("key5", "value5");

	/** using while and interator method */
	System.out.println("Method 1: ");
	Iterator<Entry<String, String>> iterator = map.entrySet().iterator();
	while (iterator.hasNext()) {
	    Map.Entry<String, String> entry = (Map.Entry<String, String>) iterator.next();
	    System.out.println(entry.getKey() + " ==> " + entry.getValue());
	}
	System.out.println();

	/** using for and keySet to get the keys and values */
	System.out.println("Method 2:");
	for (Object key : map.keySet()) {
	    System.out.println(key.toString() + " ==> " + map.get(key));
	}
	System.out.println();

	/** using for and entrySet method */
	System.out.println("Method 3: ");
	for (Map.Entry<String, String> entry : map.entrySet()) {
	    System.out.println(entry.getKey() + " ==>" + entry.getValue());
	}
	System.out.println();

	/** using Java 8, forEach and Lambda */
	System.out.println("Method 4:");
	map.forEach((k, v) -> System.out.println(k + " ==> " + v));
	System.out.println();

	System.out.println("Method 5:");
	Set<String> set = map.keySet();
	Iterator<String> keys = set.iterator();
	while (keys.hasNext()) {
	    String key = keys.next();
	    System.out.println(key + " ==> " + map.get(key));
	}
	System.out.println();

	System.out.println("Method 6: ");
	Collection<String> valuesC = map.values();
	Iterator<String> values = valuesC.iterator();
	while (values.hasNext()) {
	    String value = values.next();
	    System.out.println(value);
	}

    }
}

Output

Map loop example

Method 1: 
key1 ==> value1
key2 ==> value2
key5 ==> value5
key3 ==> value3
key4 ==> value4

Method 2:
key1 ==> value1
key2 ==> value2
key5 ==> value5
key3 ==> value3
key4 ==> value4

Method 3: 
key1 ==>value1
key2 ==>value2
key5 ==>value5
key3 ==>value3
key4 ==>value4

Method 4:
key1 ==> value1
key2 ==> value2
key5 ==> value5
key3 ==> value3
key4 ==> value4

Method 5:
key1 ==> value1
key2 ==> value2
key5 ==> value5
key3 ==> value3
key4 ==> value4

Method 6: 
value1
value2
value5
value3
value4

 

 

 

References