How to iterate Map in Java

This Example shows how to iterate Map in java . In this post we will see how to iterate TreeMap in java using for each loop , iterator and java8 . If you are new in TreeMap Then i will recommended to you see Java TreeMap Examples . There are following methods for iterate over map in java .

Iterate Map in Java

Now we will discuss several ways of iterate TreeMap in java.

 1. Java Map iterate Using entrySet() and and for each loop

In this example we will iterate treemap in java using entrySet() and for each loop . We will initialize TreeMap using put() method .We will get set of entries (Keys and Values) by calling entrySet() . We will iterate this entry set using for-each loop . print key and value pair of Map .

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

public class TreeMapIterate {
	
	public static void main(String[] args) {
        Map<String, String> treeMap = new TreeMap<String, String>();
		treeMap.put("java", "amit");
		treeMap.put("spring","ravi");
		treeMap.put("mysql","anmol");
System.out.println("Iterating TreeMap using entrySet with simple for-each loop ===>>");
	  for(Map.Entry<String, String> entry: treeMap.entrySet()) {
	       System.out.println("Key Of map = "+ entry.getKey() + 
	                           " , value of map = " + entry.getValue() );
	      }
			
	}
}

Output :

Iterating TreeMap using entrySet with simple for-each loop ===>>
Key Of map = spring , value of map = ravi
Key Of map = java , value of map = amit
Key Of map = mysql , value of map = anmol

  2. Iterate Map in Java Using keyset() and and for each loop

In this example we iterate through map java using keySet() and for each loop . We initialize TreeMap and get Set of keys from ThreeMap using keySet() . Iterate this Set of keys using for each loop and print key-value pair of Map .

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

public class TreeMapIterate {
	
	public static void main(String[] args) {
        Map<String, String> treeMap = new TreeMap<String, String>();
		treeMap.put("java", "amit");
		treeMap.put("spring","ravi");
		treeMap.put("mysql","anmol");
		
System.out.println("Iterating TreeMap using keySet with simple for-each loop ===>>");
        for(String key : treeMap.keySet()) {
            System.out.println("Key Of map = "+ key 
            		+" , value of map = " + treeMap.get(key) );
        }
			
	}
}

Output :

Iterating TreeMap using keySet with simple for-each loop ===>>
Key Of map = spring , value of map = ravi
Key Of map = java , value of map = amit
Key Of map = mysql , value of map = anmol

 3.  Iterate Over Map in Java Using entrySet() and java Iterator

In this way we iterate over map in java using entrySet() and java Iterator. Firstly we initialize Map with element . Get Set of entry from TreeMap using entrySet() then iterate this Set of entry using java iterator and print element of TreeMap . Let’s see program for loop through map in java .

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

public class TreeMapIterate {
	
	public static void main(String[] args) {
        Map<String, String> treeMap = new TreeMap<String, String>();
		treeMap.put("java", "amit");
		treeMap.put("spring","ravi");
		treeMap.put("mysql","anmol");
		
          System.out.println("=>> Iterating TreeMap using entrySet ===>>");
	  Set<Map.Entry<String, String>> employeeEntries = treeMap.entrySet();            
      Iterator<Map.Entry<String, String>> employeeIterator = employeeEntries.iterator();
	  while (employeeIterator.hasNext()) {
	         Map.Entry<String, String> entry = employeeIterator.next();
                   System.out.println("Key Of map = "+ entry.getKey() 
                              +" , value of map = " + entry.getValue() );
	          }
			
	}
}

Output :

=>> Iterating TreeMap using entrySet ===>>
Key Of map = spring , value of map = ravi
Key Of map = java , value of map = amit
Key Of map = mysql , value of map = anmol

 4. Iterate Map in Java Using keyset() and java Iterator

In this example we iterate through map in java with the help of keySet() and Iterator and we get Set Of keys from TreeMap using keySet() and iterate Set of key using Iterator and print key-value pair of TreeMap .

public class TreeMapIterate {
	
	public static void main(String[] args) {
        Map<String, String> treeMap = new TreeMap<String, String>();
		treeMap.put("java", "amit");
		treeMap.put("spring","ravi");
		treeMap.put("mysql","anmol");
		
		System.out.println("====>> Iterating TreeMap using keySet ===>>");
        Set<String> keySet = treeMap.keySet();
        Iterator<String> keysIterator = keySet.iterator();
        while (keysIterator.hasNext()) {
            String key = keysIterator.next();
     System.out.println("Key Of map = "+ key + " , value of map = " + treeMap.get(key) );
          }
			
	}
}

Output :

====>> Iterating TreeMap using keySet ===>>
Key Of map = spring , value of map = ravi
Key Of map = java , value of map = amit
Key Of map = mysql , value of map = anmol

 5. Java Map Iterate through values() Method

In this method we will iterate through only values of TreeMap . Firstly we will initialize TreeMap using put() method . Get collection view containing all the values of the map using values() method . Iterate this collection values using for loop and print all values of Map . Let’s see java program for TreeMap iteration using values() method .

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

public class TreeMapIterate {
	
	public static void main(String[] args) {
        Map<String, String> treeMap = new HashMap<String, String>();
		treeMap.put("java", "amit");
		treeMap.put("spring","ravi");
		treeMap.put("mysql","anmol");
		
System.out.println("Iterating TreeMap using values with simple for-each loop ===>>");
	        for(String value : treeMap.values()) {
	            System.out.println("Value Of map = "+ value );
	        }
			
	}
}

Output :

Iterating TreeMap using values with simple for-each loop ===>>
Value Of map = ravi
Value Of map = amit
Value Of map = anmol

6. Java Map Iterate using java 8

In this method we will do TreeMap iteration using java8. We will loop through map using forEach in java .Let’s see code for iterate TreeMap in java 8.

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

public class TreeMapIterate {
	
	public static void main(String[] args) {
        Map<String, String> treeMap = new HashMap<String, String>();
		treeMap.put("java", "amit");
		treeMap.put("spring","ravi");
		treeMap.put("mysql","anmol");
		
	    treeMap.forEach((key, value) -> {
	        System.out.println("Key Of map = "+ key +" value of map = " + value );
	     });
			
	}
}

Output :

Key Of map = spring , value of map = ravi 
Key Of map = java , value of map = amit 
Key Of map = mysql , value of map = anmol

In this example we have learned couple of method for iterating a map in java. Now you knew how to iterate through a TreeMap in java . If you are new in java collection framework i will recommended to you read collection in java with example .

Reference :

Javadoc TreeMap
Javadoc Iterator
Javadoc Map.Entry  

Leave a Reply

Your email address will not be published. Required fields are marked *

70 − 68 =