Map<Integer, String> resultMap = sortMapByKey(map); //按Key进行排序
/**
* 使用 Map按key进行排序
* @param map
* @return
*/
public Map<Integer, String> sortMapByKey(Map<Integer, String> map) {
if (map == null || map.isEmpty()) {
return null;
}
Map<Integer, String> sortMap = new TreeMap<Integer, String>(
new MapKeyComparator());
sortMap.putAll(map);
return sortMap;
}
class MapKeyComparator implements Comparator<Integer> {
@Override
public int compare(Integer integer, Integer t1) {
return integer.compareTo(t1);
}
}