1. Overview
In this tutorial, We will learn how to iterate the map in older java versions and examples in java 8.
And alos let us explore how to iterate the keys of TreeMap sorted that means in the ascending order.
TreeMap can be used where we want the map sorted based on the key by default in the ascending or descending order use cases.
2. Example to Iterate the TreeMap before JDK 8
In the below code we have created the TreeMap object and added few key value pairs. Here key is the type String and Value is type of Integer.
package com.javaprogramto.collections.treemap; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.TreeMap; public class TreeMapIterate { public static void main(String[] args) { // Creating the TreeMap object Map<String, Integer> studentsCountMap = new TreeMap<>(); // Adding students class and no of students in the class studentsCountMap.put("2nd class", 200); studentsCountMap.put("1nd class", 100); studentsCountMap.put("4nd class", 400); studentsCountMap.put("5nd class", 500); studentsCountMap.put("3nd class", 300); // Getting the Set object using keySet() method Set<String> keysSet = studentsCountMap.keySet(); // Getting the iterator object Iterator<String > it = keysSet.iterator(); // Iterating the map using regular method of iterator. while (it.hasNext()) { String key = it.next(); System.out.println("Key - "+key+", Value - "+studentsCountMap.get(key)); } } }
Output:
Key - 1nd class, Value - 100 Key - 2nd class, Value - 200 Key - 3nd class, Value - 300 Key - 4nd class, Value - 400 Key - 5nd class, Value - 500
In the above program, we have first got the all the keys of treemap using keySet() method and next used the iterator() method to get the Iterator instance.
Finally, used the traditional iterate method workflow to get the each key from iterator and passed the key to treemap to get the value of the corresponding key.
3. Java 8 Lamdba Foreach TreeMap
Next, Look at the below program to get the keys and values of TreeMap using Java 8 Lambda expressions with forEach() method.
package com.javaprogramto.collections.treemap; import java.util.Map; import java.util.TreeMap; public class TreeMapIterateJava8 { public static void main(String[] args) { // Creating the TreeMap object Map<String, Integer> studentsCountMap = new TreeMap<>(); // Adding students class and no of students in the class studentsCountMap.put("2nd class", 200); studentsCountMap.put("1nd class", 100); studentsCountMap.put("4nd class", 400); studentsCountMap.put("5nd class", 500); studentsCountMap.put("3nd class", 300); // Java 8 lambda foreach studentsCountMap.forEach((key, value) -> { System.out.println("Key - " + key + ", Value - " + value); }); } }
This ways also produces the same output as seen in the above section. This is the simplified version in java 8 and it internally uses the BiConsumer functional interface.
4. Java 8 Stream Foreach TreeMap EntrySet
Finally, explore the java 8 stream construct to get the Entry object from stream and pass it to forEach method.
package com.javaprogramto.collections.treemap; import java.util.Map; import java.util.TreeMap; public class TreeMapIterateJava8 { public static void main(String[] args) { // Creating the TreeMap object Map<String, Integer> studentsCountMap = new TreeMap<>(); // Adding students class and no of students in the class studentsCountMap.put("2nd class", 200); studentsCountMap.put("1nd class", 100); studentsCountMap.put("4nd class", 400); studentsCountMap.put("5nd class", 500); studentsCountMap.put("3nd class", 300); // java 8 Stream entry forEach studentsCountMap.entrySet().stream().forEach( entry -> System.out.println("entry key - " + entry.getKey() + ", entry value - " + entry.getValue())); } }
Output:
entry key - 1nd class, entry value - 100 entry key - 2nd class, entry value - 200 entry key - 3nd class, entry value - 300 entry key - 4nd class, entry value - 400 entry key - 5nd class, entry value - 500
5. Conclusion
In this article, We have seen how to iterate the TreeMap in older and new JDK versions with examples.
No comments:
Post a Comment
Please do not add any spam links in the comments section.