Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort HashMap basis on Object's member variable from value [duplicate]

Have one class

class Employee {
    int id;
    String name;
}

and one map which contains this object in value

Map<Integer, Employee> map = new HashMap<Integer, Employee>();

Now I want to sort above map one the basis of Employee's name. Means when I iterate this map using Map.Entry, Employee objects must retrive alphabetically.

Thanks in advance

like image 257
Navnath Avatar asked Mar 21 '13 07:03

Navnath


1 Answers

Use a TreeMap with a custom Comparator using this constructor:

http://docs.oracle.com/javase/6/docs/api/java/util/TreeMap.html#TreeMap(java.util.Comparator)

like image 143
anubhava Avatar answered Oct 13 '22 05:10

anubhava