If you want to save the elements in the SortedSet in the reverse order, the only change you need to make is to build the TreeSet using the appropriate constructor , which the custom Comparator accepts:
Map<Float, String> mylist = new HashMap<>(); mylist.put(10.5, a); mylist.put(12.3, b); mylist.put(5.1, c); SortedSet<Float> orderlist = new TreeSet<Float>(Collections.reverseOrder()); orderList.addAll(mylist.keySet()); for (Float i : orderlist) { System.out.println(i+" "+mylist.get(i)); }
Note that the neat method here is Collections.reverseOrder() , which returns a Comparator that compares as opposed to the natural ordering of the elements.
source share