Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

sort hashmap by value using java 1.4

807607Nov 21 2006 — edited Nov 21 2006
I have to following HashMap



Map students = new HashMap();
students.put("A", new Integer(12));
students.put("B", new Integer(60));
students.put("C", new Integer(80));
students.put("D", new Integer(71));
students.put("E", new Integer(30));
students.put("F", new Integer(45));


By making a TreeMap I can sort the Map on the basis of keys, but how should I sort the Map on the basis of values.
The key value pairing should remain intact.
If I use the sort method and then put the values back, I'll get

A, 12
B, 30
C, 45
D, 60
E, 71
F, 80
In this case the key value combination is distorted.
What I need is

A, 12
E, 30
F, 45
B, 60
D, 71
C, 80

Say this example is students and their marks Map. A scored 12, B scored 60..
I want to sort the students on the basis of their marks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 19 2006
Added on Nov 21 2006
21 comments
1,390 views