目录
原始的hashmap如下所示:
Map<String,Integer> map = new HashMap<>();
map.put("key1",1);
map.put("key3",2);
map.put("key2",3);
根据key排序
1.将map的key取出来,并且进行排序
Set<String> keySet = map.keySet();
Object[] keyArr = keySet.toArray();
Arrays.sort(keyArr);
for (Object key: keyArr){
System.out.println(key);
}
结果如下:
key1
key2
key3
2.将map的entryset取出,并且转换为list,之后根据Collections的sort排序
List<Map.Entry<String, Integer>> mapList = new ArrayList<>(map.entrySet());
Collections.sort(mapList, new Comparat