TreeMap with Looping

We can iterate TreeMap using enhanced for loop with keyset() method or by using enhanced for loop with EntrySet method or by using iterator

Case 1

Enhanced for loop with keyset()

Example

  1. import java.util.*;
  2. public class TreeMapEnhancedForLoopExample {
  3. public static void main(String args[]) {
  4.  
  5. TreeMap<Integer,String> tm = new TreeMap<>();
  6. tm.put(3, “Three”);
  7. tm.put(1, “One”);
  8. tm.put(2, “Two”);
  9.  
  10. Set<Integer> keys = tm.keySet();
  11. for(Integer key: keys){
  12. System.out.println(“Value of “+key+” is:+tm.get(key));
  13. }
  14. }
  15. }
import java.util.*;
public class TreeMapEnhancedForLoopExample {
public static void main(String args[]) {

TreeMap<Integer,String> tm = new TreeMap<>();
tm.put(3, “Three”);
tm.put(1, “One”);
tm.put(2, “Two”);

Set<Integer> keys = tm.keySet();
for(Integer key: keys){
System.out.println(“Value of “+key+” is: “+tm.get(key));
}
}
}


In this program, We have accessed all the keys of TreeMap using keySet( ) method and then iterated all the keys using enhanced for loop and accessed value by passing key in each iteration in the loop.

Case 2

Enhanced for loop with entrySet()

Example

  1. import java.util.*;
  2. public class TreeMapEnhancedForLoopEntrySetExample {
  3. public static void main(String args[]) {
  4.  
  5. TreeMap<Integer,String> tm = new TreeMap<>();
  6. tm.put(2, “Two”);
  7. tm.put(1, “One”);
  8. tm.put(3, “Three”);
  9. for (Map.Entry entry : tm.entrySet()) {
  10. Integer key = entry.getKey();
  11. Object value = entry.getValue();
  12. System.out.println(“Value of “+key+” is:+value);
  13. }
  14.  
  15. }
  16. }
import java.util.*;
public class TreeMapEnhancedForLoopEntrySetExample {
public static void main(String args[]) {

TreeMap<Integer,String> tm = new TreeMap<>();
tm.put(2, “Two”);
tm.put(1, “One”);
tm.put(3, “Three”);
for (Map.Entry entry : tm.entrySet()) {
Integer key = entry.getKey();
Object value = entry.getValue();
System.out.println(“Value of “+key+” is: “+value);
}

}
}


Case 3

Using iterator

Example

  1. import java.util.*;
  2. public class TreeMapIteratorExample {
  3. public static void main(String args[]) {
  4.  
  5. TreeMap<Integer,String> tm = new TreeMap<>();
  6. tm.put(3, “Three”);
  7. tm.put(1, “One”);
  8. tm.put(2, “Two”);
  9.  
  10. Iterator it = tm.entrySet().iterator();
  11. while (it.hasNext()) {
  12. Map.Entry pair = (Map.Entry)it.next();
  13. System.out.println(pair.getKey() +=+ pair.getValue());
  14.  
  15. }
  16.  
  17. }
  18. }
import java.util.*;
public class TreeMapIteratorExample {
public static void main(String args[]) {

TreeMap<Integer,String> tm = new TreeMap<>();
tm.put(3, “Three”);
tm.put(1, “One”);
tm.put(2, “Two”);

Iterator it = tm.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
System.out.println(pair.getKey() + ” = ” + pair.getValue());

}

}
}

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on