private Map<Character, Integer> frequencies;
I have a Map with Character being Key and its associated Integer being Value.
Whats the best/fastest/efficeint way to sort by Value?
i.e Map may have
a,1
c,10
p,5
s,7
and after sorted,
it would be
a,1
p,5
s,7
c,10
I was thinking about doing it with Priority Queue and with integer but i would lose the Character value if the integer vals are duplicates
A priority queue is a decent approach – all you need to do is get the
Entryset from the map, and override aComparatoras the input to the queue.Will yield (as expected):
Note: Avoid using a
Setor aMapwith keys as the values of the map – because it will NOT handle duplicate values well.