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 :
- Using keySet(); method and for-each loop
- Using keySet(); method and Iterator interface
- Using entrySet(); method and for-each loop
- Using entrySet(); method and Iterator interface
- 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 :
- Java – Various ways to iterate through ArrayList
- Java – Various ways to iterate through Vector – 5 ways
- Java – Various ways to iterate through LinkedList – 5 ways
- Java – Various ways to iterate through HashSet – 3 ways
- Java – How to reverse LinkedHashSet contents ?
- Java – Various ways to iterate through TreeSet – 3 ways
- Java – Various ways to iterate over List of HashMap
- Java – Ways to iterate over HashMap of ArrayList
- Java – Various ways to iterate Arrays – 5 ways
- Java – How to remove elements while iterating collection object ?
- Java – How to add/remove/modify an element in List while iterating ?
References :
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Map.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html
- https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
- https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/set.html
- https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html
- https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html
- https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html
Happy Coding !!
Happy Learning !!