Different ways to iterate TreeMap in Java

Using Iterator – Iterating HashMap using an iterator.

package iteratingtreemap;

import java.util.*;

 
class HashMapExample {
	public static void main(String[] arg) {
		Map<Integer, String> treeMapObject = new TreeMap<>();
 
		treeMapObject.put(2, "ram");
		treeMapObject.put(3, "mohan");
		treeMapObject.put(4, "Bangalore");
		treeMapObject.put(5, "Rakesh");
 
		// we will iterate using Iterator
		Iterator<Map.Entry<Integer, String>> iterator = treeMapObject
				.entrySet().iterator();
 
		while (iterator.hasNext()) {
			Map.Entry<Integer, String> entry = iterator.next();
			System.out.println("Key is = " + entry.getKey() + ", Value is = "
					+ entry.getValue());
		}
	}
}

Output is –

Key is = 2, Value is = ram
Key is = 4, Value is = Bangalore
Key is = 5, Value is = Rakesh
Key is = 8, Value is = mohan

Using for each loop -Iterating HashMap using for each loop.

package iteratingtreemap;



import java.util.*;

 
public class Example2 {
	public static void main(String[] args) {
		Map<Integer, String> treeMapObj = new TreeMap<>();
 
		treeMapObj.put(2, "ram");
		treeMapObj.put(5, "mohan");
		treeMapObj.put(3, "sohan");
 
		Set<Map.Entry<Integer, String>> entrySet = treeMapObj.entrySet();
 
		for (Map.Entry<Integer, String> entry : entrySet) {
			System.out.println("Key is : " + entry.getKey() + "   Value is : "
					+ entry.getValue());
		}
	}
}

Output is –

Key is : 2 Value is : ram
Key is : 3 Value is : sohan
Key is : 5 Value is : mohan

Using Lambda expression – Iterating HashMap using the lambda expression.

package iteratingtreemap;

import java.util.*;

 
public class Example3 {
	public static void main(String[] args) {
		Map<Integer, String> treeMapObj = new TreeMap<>();
 
		treeMapObj.put(2, "ram");
		treeMapObj.put(5, "mohan");
		treeMapObj.put(3, "sohan");
		treeMapObj.put(9, "rohit");
 
		treeMapObj.forEach((k, v) -> {
			System.out.println("Key is : " + k + "   Value is : " + v);
		});
 
	}
}

Output is –

Key is : 2 Value is : ram
Key is : 3 Value is : sohan
Key is : 5 Value is : mohan
Key is : 9 Value is : rohit


Using Java 8 stream() method –


package iteratingtreemap;

import java.util.*;

 
public class Example4 {
	public static void main(String[] args) {
		Map<Integer, String> treeMapObj = new TreeMap<>();
 
		treeMapObj.put(2, "ram");
		treeMapObj.put(5, "mohan");
		treeMapObj.put(3, "sohan");
		treeMapObj.put(9, "rohit");
 
		Set<Map.Entry<Integer, String>> setReference= treeMapObj.entrySet();
 
		setReference.stream().forEach
		(mapEntry-> System.out.println("Key is : "+mapEntry.getKey()+"   Value is : "+mapEntry.getValue()));
	}
}

Output is –

Key is : 2 Value is : ram
Key is : 3 Value is : sohan
Key is : 5 Value is : mohan
Key is : 9 Value is : rohit

Using the keySet() and Iterator – 

package iteratingtreemap;

import java.util.*;

 
public class Example5 {
	public static void main(String[] args) {
		Map<Integer, String> treeMapObj = new TreeMap<>();
 
		treeMapObj.put(2, "ram");
		treeMapObj.put(5, "mohan");
		treeMapObj.put(3, "sohan");
		treeMapObj.put(9, "rohit");
 
		Iterator iterator = treeMapObj.keySet().iterator();
 
		while (iterator.hasNext()) {
			Integer key = (Integer) iterator.next();
 
			System.out.println("Key is : " + key + "   Value is : " + treeMapObj.get(key));
		}
	}
}

Output is –

Key is : 2 Value is : ram
Key is : 3 Value is : sohan
Key is : 5 Value is : mohan
Key is : 9 Value is : rohit

 

Using keySet() and for each loop –

package iteratingtreemap;

import java.util.*;

 
public class Example6 {
	public static void main(String[] args) {
		Map<Integer, String> treeMapObj = new TreeMap<>();
 
		treeMapObj.put(2, "ram");
		treeMapObj.put(5, "mohan");
		treeMapObj.put(3, "sohan");
		treeMapObj.put(9, "rohit");
 
		for (Integer key : treeMapObj.keySet()) {
			System.out.println("Key is : " + key + "   Value is : " + treeMapObj.get(key));
		}
	}
}

Output is –

Key is : 2 Value is : ram
Key is : 3 Value is : sohan
Key is : 5 Value is : mohan
Key is : 9 Value is : rohit