I have a problem with HashMap.
I want to put into map:
key - 320, value - 0.1
key - 321, value - 0.7
key - 322, value - 0.5
key - 323, value - 0.6
After that I want to sort this map in descending order like this:
key - 321, value - 0.7
key - 323, value - 0.6
key - 322, value - 0.5
key - 320, value - 0.1
and after that I want to sort map in ascending order, but only values:
key - 321, value - 0.1
key - 323, value - 0.5
key - 322, value - 0.6
key - 320, value - 0.7
This is possible in hashmaps? How I can do this?
No. HashMap is an implementation of Map insteface that does not preserve any order of keys. It puts keys according to their hash code.
But you have easier solution. You should use TreeMap instead. This class accepts comparator. Use it with your own comparator and you even do not have to sort anything. The keys will be returned according to the comparator. To inverse order you can either use other TreeMap or just say
new ArrayList(t.keySet())and then inverse the order of the list.