This example shows how to iterate TreeMap in Java. This example also shows how to iterate TreeMap keys, values, and entries using for, forEach, and Iterator.
How to iterate TreeMap in Java?
There are various ways using which you can iterate through TreeMap keys, value or entries as given below.
1. How to iterate all keys of TreeMap?
The keySet
method of the TreeMap class returns a Set view of all the keys stored in the TreeMap object. We can then iterate through keys using below given ways.
Using for loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
TreeMap<Integer, String> tmapNumbers = new TreeMap<Integer, String>(); tmapNumbers.put(1, "One"); tmapNumbers.put(2, "Two"); tmapNumbers.put(3, "Three"); /* * Get all TreeMap keys */ Set<Integer> keys = tmapNumbers.keySet(); for(Integer key : keys){ System.out.println(key); } |
Using an Iterator
1 2 3 4 5 6 7 8 |
Set<Integer> keys = tmapNumbers.keySet(); //get an iterator Iterator<Integer> iterator = keys.iterator(); while(iterator.hasNext()){ System.out.println( iterator.next() ); } |
Using forEach
1 2 3 4 5 |
Set<Integer> keys = tmapNumbers.keySet(); keys.forEach( key -> { System.out.println( key ); }); |
Output
1 2 3 |
1 2 3 |
2. How to iterate all values of TreeMap?
The values
method of the TreeMap class returns a Collection view of all the values stored in the TreeMap object. We can then iterate through all the values using below given ways.
Using for loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
TreeMap<Integer, String> tmapNumbers = new TreeMap<Integer, String>(); tmapNumbers.put(1, "One"); tmapNumbers.put(2, "Two"); tmapNumbers.put(3, "Three"); /* * Get all TreeMap values */ Collection<String> values = tmapNumbers.values(); for(String value : values){ System.out.println( value ); } |
Using an Iterator
1 2 3 4 5 6 7 8 |
Collection<String> values = tmapNumbers.values(); //get an iterator Iterator<String> iterator = values.iterator(); while(iterator.hasNext()){ System.out.println( iterator.next() ); } |
Using forEach
1 2 3 4 5 |
Collection<String> values = tmapNumbers.values(); values.forEach( value -> { System.out.println( value ); }); |
Output
1 2 3 |
One Two Three |
3. How to iterate all entries of TreeMap?
The entrySet
method of the TreeMap class returns a Set view of all the entries contained in the TreeMap object. We can then iterate through the entries using below given ways.
Using the for loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
TreeMap<Integer, String> tmapNumbers = new TreeMap<Integer, String>(); tmapNumbers.put(1, "One"); tmapNumbers.put(2, "Two"); tmapNumbers.put(3, "Three"); /* * Get all entries */ Set<Map.Entry<Integer, String>> entries = tmapNumbers.entrySet(); for(Map.Entry<Integer, String> entry : entries){ System.out.println( entry.getKey() + "->" + entry.getValue() ); } |
Using an Iterator
1 2 3 4 5 6 7 8 9 10 11 |
Set<Map.Entry<Integer, String>> entries = tmapNumbers.entrySet(); //get an iterator Iterator<Map.Entry<Integer, String>> iterator = entries.iterator(); Map.Entry<Integer, String> entry = null; while(iterator.hasNext()){ entry = iterator.next(); System.out.println( entry.getKey() + "->" + entry.getValue() ); } |
Using the forEach
1 2 3 4 5 |
Set<Map.Entry<Integer, String>> entries = tmapNumbers.entrySet(); entries.forEach( entry -> { System.out.println( entry.getKey() + "->" + entry.getValue() ); }); |
Output
1 2 3 |
1->One 2->Two 3->Three |
Please also visit Java TreeMap forEach example.
This example is a part of the TreeMap in Java Tutorial.
Please let me know your views in the comments section below.
References:
Java 8 TreeMap