This example shows how to iterate Java TreeMap in reverse order. The example also shows how to iterate TreeMap in reverse order using the reverseOrder, descendingKeySet, and descendingMap methods.
How to iterate TreeMap in reverse order in Java?
There are several ways using which you can iterate the TreeMap object in the reverse direction as given below.
1. Using the reverseOrder method of the Collections class
The reverseOrder
method of the Collections class returns a Comparator that imposes the reverse of the natural ordering of the objects. We can then use this comparator object in the constructor of the TreeMap to create an object that stores the mapping in the reverse order of the keys.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import java.util.Collections; import java.util.TreeMap; public class TreeMapIterateReverseExample { public static void main(String[] args) { /* * Create TreeMap object that sorts the entries * using the reverse order */ TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>( Collections.reverseOrder() ); tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, "Blue"); /* * TreeMap entries will be automatically sorted in the * reverse order of the keys */ System.out.println("TreeMap contains: " + tmapColors); } } |
Output
1 |
TreeMap contains: {3=Blue, 2=Green, 1=Red} |
Note: The above given approach works at the TreeMap object creation time. If you have an existing map object that is not created using the reverseOrder
method, you can use the below given approaches to iterate the map in reverse order.
2. Using the descendingKeySet method of the TreeMap class
The descendingKeySet
method returns a reverse order Set view of the keys contained in the TreeMap object.
1 |
public NavigableSet<K> descendingKeySet() |
The iterator of the set view returns the TreeMap keys in the descending order.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, "Blue"); /* * Use the descendingKeySet method to get a set * view of keys in descending order */ Set<Integer> reverseKeys = tmapColors.descendingKeySet(); /* * This iterator will return keys in the descending or * reverse order */ Iterator<Integer> iterator = reverseKeys.iterator(); Integer key = null; while(iterator.hasNext()){ key = iterator.next(); System.out.println( key + "->" + tmapColors.get(key) ); } |
Output
1 2 3 |
3->Blue 2->Green 1->Red |
Important Note:
The key set returned by the descendingKeySet
method is a view and it is backed by the original TreeMap object. Any changes you make to this view will be reflected to the original TreeMap object and vice versa.
Plus, this set view does not support add
and addAll
operations. If you call the add
or addAll
method on the set view, the code will throw java.lang.UnsupportedOperationException exception.
3. Using the descendingMap method of the TreeMap class
The descendingMap
method of the TreeMap class returns a map containing reverse view of the mappings contained in this TreeMap object.
1 |
public NavigableMap<K,V> descendingMap() |
Once you get this map, you can iterate through the entries using the iterator of the entry set.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
TreeMap<Integer, String> tmapColors = new TreeMap<Integer, String>(); tmapColors.put(1, "Red"); tmapColors.put(2, "Green"); tmapColors.put(3, "Blue"); /* * Use the descendingMap method to get a map containing * reverse view of mapping */ Map<Integer, String> reverseMap = tmapColors.descendingMap(); /* * Get an iterator for all the entries */ Iterator<Map.Entry<Integer, String>> iterator = reverseMap.entrySet().iterator(); Map.Entry<Integer, String> entry = null; while(iterator.hasNext()){ entry = iterator.next(); System.out.println( entry.getKey() + "->" + entry.getValue() ); } |
Output
1 2 3 |
3->Blue 2->Green 1->Red |
Note: The descending map returned by the descendingMap
method is a view so any changes you make will be reflected in the original TreeMap and vice versa.
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