Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order HashMap alphabetically by value [duplicate]

I have a HashMap<Object, Student> where the Object is the ID of the Student, and the Student is an object from Student.

How can I resort the HashMap by the Students name, student->getName()?

like image 277
Chiggins Avatar asked Nov 10 '10 23:11

Chiggins


1 Answers

HashMaps are intrinsically unordered and cannot be sorted.

Instead, you can use a SortedMap implementation, such as a TreeMap.
However, even a sorted map can only sort by its keys.

If you want to sort by the values, you'll need to copy them to a sorted list.

like image 127
SLaks Avatar answered Nov 12 '22 07:11

SLaks