SortedMap interface extends Map and ensures that all entries are in an ascending key order (hence SortedMap).
If you want to have it in a descending order, you will need to override the Compare method in the SortedMap which we will do shortly. TreeMap implements SortedMap and it either orders the keys by their natural order or by a specified comparator.In TreeMap null keys and null values are not permitted.
Methods summary
- Comparator <? super K> comparator(): returns the comparator that is used to order the keys in the current map or null if the current map uses the natural ordering of its keys.
- Set<Map.Entry<K,V>> entrySet(): returns a Set view of the mappings contained in the current map.
- K firstKey(): returns the first key currently in the map (either lowest or highest, depends on the way you implemented the map (ascending or descending)).
- SortedMap<K,V> headMap(K toKey): returns a view of the portion of the current map whose keys are strictly less than toKey.
- Set<K> keySet(): returns a Set view of the keys contained in the current map
- K lastKey(): returns the last (highest or lowest) key currently in the map
- SortedMap<K,V> subMap(K fromKey, K toKey): returns a view of the portiong of the current map whose leys are ranging from fromKey to toKey exclusively
- SortedMap<K,V> tailMap(K fromKey): returns a view of the portion of the current map whose keys are greater than or eqaul to fromKey
- Collection <V> values(): returns a Collection view of the values contained in the current map
For more details on the methods, check out the official Oracle documentation.
Code implementation
import java.util.*; public class SortedHashMapExample { public static void main(String args[]) { Map<Double, String> players = new TreeMap<Double, String>(); // health, name players.put(new Double(100.00), "Hill"); players.put(new Double(120.00), "John"); players.put(new Double(150.00), "Sabrina"); players.put(new Double(105.00), "Caitlyn"); players.put(new Double(110.00), "Rachel"); players.put(new Double(130.00), "Michael"); players.put(new Double(140.00), "Mark"); // get a set of the entries Set setOfEntries = players.entrySet(); // get an iterator Iterator iterator = setOfEntries.iterator(); while(iterator.hasNext()) { // create an entry of the map Map.Entry entry = (Map.Entry)iterator.next(); System.out.println("Key: " + entry.getKey()); System.out.println("Value: " + entry.getValue()); } } }
Output
Key: 100.0 Value: Hill Key: 105.0 Value: Caitlyn Key: 110.0 Value: Rachel Key: 120.0 Value: John Key: 130.0 Value: Michael Key: 140.0 Value: Mark Key: 150.0 Value: Sabrina
As you can see, it groups them automatically in ascending order. It starts from 100.00 health up until 150.00. The reason I put health as a key and name as a value was just to show you that it ascends them.
But what if we wanted to have them in a descending order?
Implementation using descending order
import java.util.*; public class SortedHashMapExample { public static void main(String args[]) { Map<Double, String> players = new TreeMap<Double, String>(new Comparator<Double>() { @Override public int compare(Double x, Double y) { return y.compareTo(x); } }); // name, health players.put(new Double(100.00), "Hill"); players.put(new Double(120.00), "John"); players.put(new Double(150.00), "Sabrina"); players.put(new Double(105.00), "Caitlyn"); players.put(new Double(110.00), "Rachel"); players.put(new Double(130.00), "Michael"); players.put(new Double(140.00), "Mark"); // get a set of the entries Set setOfEntries = players.entrySet(); // get an iterator Iterator iterator = setOfEntries.iterator(); while(iterator.hasNext()) { // create an entry of the map Map.Entry entry = (Map.Entry)iterator.next(); System.out.println("Key: " + entry.getKey()); System.out.println("Value: " + entry.getValue()); } } }
Output
Key: 150.0 Value: Sabrina Key: 140.0 Value: Mark Key: 130.0 Value: Michael Key: 120.0 Value: John Key: 110.0 Value: Rachel Key: 105.0 Value: Caitlyn Key: 100.0 Value: Hill
There you go. Couldn’t be easier right? What we did was just overriding the compare method and instead of x => y (ascending order), we changed it to y => x (descending order).