Java – 5 ways to iterate through TreeMap

In this article, we will discuss various ways to iterate through TreeMap. We will list down various Map implementation classes and their key property like element/objects storing-order

1. Map implementation classes :

  • HashMap –> stores Key-Value pairs (or entries) in random-order
  • LinkedHashMap –> stores Key-Value pairs (or entries) as per insertion-order
  • TreeMap –> stores Key-Value pairs (or entries) as per natural sorting-order

2. Ways to iterate through TreeMap :

  1. Using keySet(); method and for-each loop
  2. Using keySet(); method and Iterator interface
  3. Using entrySet(); method and for-each loop
  4. Using entrySet(); method and Iterator interface
  5. Using forEach(); loop introduced in Java 1.8 version

3. Demo – 5 ways to iterate through TreeMap :

DifferentWaysToIterateTreeMap.java

package in.bench.resources.collection;

import java.util.TreeMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

public class DifferentWaysToIterateTreeMap {

	public static void main(String[] args) {

		// creating TreeMap object of type <String, String>
		TreeMap<String, String> tMap = new TreeMap<String, String>();

		// adding key-value pairs to TreeMap object
		tMap.put("USA", "USD");
		tMap.put("India", "INR");
		tMap.put("UK", "GBP");
		tMap.put("Japan", "JPY");
		tMap.put("UAE", "AED");

		// Way 1: Get keySet() and Iterate using for-each loop
		System.out.println("Way 1: Get keySet() and"
				+ " Iterate using for-each loop\n");

		// getting keySet() into Set
		Set<String> set1 = tMap.keySet();

		// for-each loop
		for(String key : set1) {
			System.out.println("Key : "  + key + "\t\t"
					+ "Value : "  + tMap.get(key));
		}

		// Way 2: Get keySet() and Iterate using Iterator interface
		System.out.println("\n\nWay 2: Get keySet() and"
				+ " Iterate using Iterator interface\n");

		// getting keySet() into Set
		Set<String> set2 = tMap.keySet();

		// Collection Iterator
		Iterator<String> itr1 = set2.iterator();

		while(itr1.hasNext()) {
			String key = itr1.next();
			System.out.println("Key : "  + key + "\t\t"
					+ "Value : "  + tMap.get(key));
		}

		// Way 3: Get entrySet() and Iterate using for-each loop
		System.out.println("\n\nWay 3: Get entrySet() and"
				+ " Iterate using for-each loop\n");

		// getting entrySet() into Set
		Set<Entry<String, String>> entrySet1 = tMap.entrySet();

		// for-each loop
		for(Entry<String, String> entry1 : entrySet1) {
			System.out.println("Key : "  + entry1.getKey() + "\t\t"
					+ "Value : "  + entry1.getValue());
		}

		// Way 4: Get entrySet() and Iterate using Iterator interface
		System.out.println("\n\nWay 4: Get entrySet() and"
				+ " Iterate using Iterator interface\n");

		// getting entrySet() into Set
		Set<Entry<String, String>> entrySet2 = tMap.entrySet();

		// Collection Iterator
		Iterator<Entry<String, String>> itr2 = entrySet2.iterator();

		while(itr2.hasNext()) {
			Entry<String, String> entry2 = itr2.next();
			System.out.println("Key : "  + entry2.getKey() + "\t\t"
					+ "Value : "  + entry2.getValue());
		}

		// Way 5: Iterating Map using forEach() in Java 1.8
		System.out.println("\n\nWay 5: Iterating TreeMap using"
				+ " forEach() in Java 1.8\n");
		tMap.forEach((key, value)->System.out.println(
				"Country : " + key + "\t\t"
						+ "Currency : " + value));
	}
}

Output:

Way 1: Get keySet() and Iterate using for-each loop

Key : India		Value : INR
Key : Japan		Value : JPY
Key : UAE		Value : AED
Key : UK		Value : GBP
Key : USA		Value : USD


Way 2: Get keySet() and Iterate using Iterator interface

Key : India		Value : INR
Key : Japan		Value : JPY
Key : UAE		Value : AED
Key : UK		Value : GBP
Key : USA		Value : USD


Way 3: Get entrySet() and Iterate using for-each loop

Key : India		Value : INR
Key : Japan		Value : JPY
Key : UAE		Value : AED
Key : UK		Value : GBP
Key : USA		Value : USD


Way 4: Get entrySet() and Iterate using Iterator interface

Key : India		Value : INR
Key : Japan		Value : JPY
Key : UAE		Value : AED
Key : UK		Value : GBP
Key : USA		Value : USD


Way 5: Iterating TreeMap using forEach() in Java 1.8

Country : India		Currency : INR
Country : Japan		Currency : JPY
Country : UAE		Currency : AED
Country : UK		Currency : GBP
Country : USA		Currency : USD

4. Important points to remember about TreeMap :

  • Allows only unique keys to be stored
  • While iterating, keys are retrieved in natural sorting-order
  • Maximum of one null object is allowed, till Java 1.6 version
  • Starting Java 1.7 version, even one null object isn’t allowed,
  • Throws java.lang.NullPointerException for null object added to Map object, if you are using JDK version greater than 1.6

Related Articles :

References :

Happy Coding !!
Happy Learning !!

Java - How to sort LinkedHashSet contents ?
Java - Iterate through TreeSet in 3 ways