You can't sort a HashMap
, but you can sort its entries obtained with entrySet()
.
public class MapSort {
private static class Employee {
public String name;
public Employee(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
public static void main(String[] args) {
Map<Integer, Employee> map = new HashMap<Integer, Employee>();
map.put(1, new MapSort.Employee("x"));
map.put(2, new MapSort.Employee("a"));
map.put(3, new MapSort.Employee("f"));
List<Map.Entry<Integer, Employee>> entryList = new ArrayList<Map.Entry<Integer, Employee>>(map.entrySet());
Collections.sort(
entryList, new Comparator<Map.Entry<Integer, Employee>>() {
@Override
public int compare(Map.Entry<Integer, Employee> integerEmployeeEntry,
Map.Entry<Integer, Employee> integerEmployeeEntry2) {
return integerEmployeeEntry.getValue().name
.compareTo(integerEmployeeEntry2.getValue().name);
}
}
);
System.out.println(entryList);
}
}
After sorting you can put back your entries in a map that supports ordering, for example LinkedHashMap.
It depends on your use case: if you need to keep the map always sorted it's simpler to employ a TreeMap
that comes with an additional overhead. If you need just an one time sorting, you can use a HashMap
with the above code.