A TreeMap is probably the most straightforward way of doing this. You use it exactly like a normal Map. i.e.
Map<Float,String> mySortedMap = new TreeMap<Float,MyObject>();
// Put some values in it
mySortedMap.put(1.0f,"One");
mySortedMap.put(0.0f,"Zero");
mySortedMap.put(3.0f,"Three");
// Iterate through it and it'll be in order!
for(Map.Entry<Float,String> entry : mySortedMap.entrySet()) {
System.out.println(entry.getValue());
} // outputs Zero One Three
TreeMap sorts by the key natural ordering. The keys should implement Comparable or be compatible with a Comparator (if you passed one instance to constructor). In you case, Float already implements Comparable so you don't have to do anything special.
You can call keySet to retrieve all the keys in ascending order.
If you always want this create the map to use descending order in general, if you only need it once create a TreeMap with descending order and put all the data from the original map in.
// Create the map and provide the comparator as a argument
Map<Float, String> dscsortedMAP = new TreeMap<Float, String>(new Comparator<Float>() {
@Override
public int compare(Float o1, Float o2) {
return o2.compareTo(o1);
}
});
dscsortedMAP.putAll(ascsortedMAP);
You would only need to use SortedMap if using 3rd-party implementations that have not yet declared their support of NavigableMap. Of the maps bundled with Java, both of the implementations that implement SortedMap also implement NavigableMap.
Interface versus concrete class
s SortedMap the best answer? TreeMap?
As others mentioned, SortedMap is an interface while TreeMap is one of multiple implementations of that interface (and of the more recent NavigableMap.
Having an interface allows you to write code that uses the map without breaking if you later decide to switch between implementations.
NavigableMap< Employee , Project > currentAssignments = new TreeSet<>() ;
currentAssignments.put( alice , writeAdCopyProject ) ;
currentAssignments.put( bob , setUpNewVendorsProject ) ;
This code still works if later change implementations. Perhaps you later need a map that supports concurrency for use across threads. Change that declaration to:
NavigableMap< Employee , Project > currentAssignments = new ConcurrentSkipListMap<>() ;
…and the rest of your code using that map continues to work.
Choosing implementation
There are ten implementations of Map bundled with Java 11. And more implementations provided by 3rd parties such as Google Guava.
Here is a graphic table I made highlighting the various features of each. Notice that two of the bundled implementations keep the keys in sorted order by examining the key’s content. Also, EnumMap keeps its keys in the order of the objects defined on that enum. Lastly, the LinkedHashMap remembers original insertion order.
Because TreeMap elements are required to be ordered the Objects which are used as a key need to have a implementation of the Comparable interface. For example the following code will throw a runtime error:
class Human{}
Map<Human, String> map = new TreeMap<>();
// Following throws a exception:
// Exception in thread "main" java.lang.ClassCastException:
// class Example$1Human cannot be cast to class java.lang.Comparable
map.put(new Human(), "baz");