Greetings. I can not understand in any way whether TreeMap can be sorted by descending key without using cycles and all that, for example, simply passing a comparator into it?

My map:

Map<Integer, Integer> returnMap = new TreeMap<Integer, Integer>() 

I tried:

 Map<Integer, Integer> returnMap = new TreeMap<Integer, Integer>(new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o2 - o1; } }); 

    1 answer 1

    Pass the Comparator to the constructor.

      Map<Integer, String> map = new TreeMap<Integer, String>(Collections.reverseOrder());