How to iterate through TreeMap

This Example shows how to iterate through TreeMap in java . In this post we will see how to iterate over  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 .Now we will discuss several ways of how to loop TreeMap in java .

 1. Iterate Map Using EntrySet() and for each loop

In this way we Iterate Java Map Entries (Keys and Values) with the help for each loop . We will iterate Map using entrySet() with the help of for-each loop.

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 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 Using keyset() and for each loop

Here we Iterate Map Keys Only with the help of for each loop and get value from TreeMap in java .

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 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 Map Using EntrySet() and java Iterator

In this way we Iterate Java Map Entries (Keys and Values) with the help Iterator and print TreeMap 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 HashMap<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 Using keyset() and java Iterator

Here we Iterate Map Keys Only with the help of Iterator and get value from TreeMap in java

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 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. Iterate through values of a TreeMap

In this way we will see how to iterate through values of a Map and iterate through only values of TreeMap.

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.  Iterate Map using java 8

In this method we will iterate Treemap using java8. 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 learn couple of ways for iterating TreeMap . 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 *

83 − = 74