Map sorting based on Values (instead of keys)

These days one of the very popular interview question is regarding having Map type collection with sorting based on values (Map is key->value pair). TreeMap can give you Key based sorting.
Use case would be We have Student object (with student name and roll number properties) and we have Result object (having int result property). We have to use Student as key and Result as value in our Map but requirement is Map should be having sorting order based on values(result).
I have tried to solve this using below program, Please suggest if there is better way or this can be improved.
Result.java
package collection;
public class Result implements Comparable{
private int marks;
public int getMarks() {
return marks;
}
public void setMarks(int marks) {
this.marks = marks;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + marks;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Result other = (Result) obj;
if (marks != other.marks)
return false;
return true;
}
@Override
public int compareTo(Object arg0) {
if(this.getMarks()> ((Result)arg0).getMarks())
return 1;
else if(this.getMarks()< ((Result)arg0).getMarks())
return -1;
else return 0;
}
Result(int lmarks){
this.marks = lmarks;
}
@Override
public String toString() {
return "Result [marks=" + marks + "]";
}
}
Student.java
package collection;
public class Student{
private int id;
private String name;
//Getters and setters
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
}
public Student(int id, String name){
this.id=id;
this.name=name;
}
MapSortingDemo.java
package collection;
import java.util.*;
import java.util.Map.Entry;
public class MapSortingDemo {
public static void main(String[] args) {
Student s1 = new Student(10,"Amit");
Student s2 = new Student(15,"AMMM");
Student s3 = new Student(5,"MMM");
Result r1 = new Result(65);
Result r2 = new Result(75);
Result r3 = new Result(55);
Map<Student, Result> m = new HashMap<Student, Result>();
m.put(s1, r1);
m.put(s2, r2);
m.put(s3, r3);
Map <Student, Result> sortedonValues = sortByComparator(m);
System.out.println(sortedonValues);
}
@SuppressWarnings("unchecked")
private static Map<Student,Result> sortByComparator(Map<Student, Result> unsortMap) {
List <Student>list = new LinkedList(unsortMap.entrySet());
// sort list based on comparator
Collections.sort(list, new Comparator() {
@SuppressWarnings("rawtypes")
public int compare(Object o1, Object o2) {
return ((Comparable) ((Map.Entry) (o1)).getValue())
.compareTo(((Map.Entry) (o2)).getValue());
}
});
// put sorted list into map again
//LinkedHashMap make sure order in which keys were inserted
Map sortedMap = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
}
Output:
{Student [id=5, name=MMM]=Result [marks=55], Student [id=10, name=Amit]=Result [marks=65], Student [id=15, name=AMMM]=Result [marks=75]}
Suggestion and alternate methods welcome. Happy coding.