HashMap<String,Integer> hm = new HashMap<>();
hm.put("a", 1);
hm.put("c", 2);
hm.put("b", 3);
Set<Entry<String, Integer>> entrySet = hm.entrySet();
Entry<String, Integer>[] array = entrySet.toArray(new Entry[entrySet.size()]);
//唯一绕过限制,创建泛型数组的方式,是先创建一个原生类型数组,然后再强制转型。
//不能new Entry<String, Integer>[entrySet.size()]
//前边Entry<String, Integer>,<String, Integer>不能去掉,否则下面sort报错
Arrays.sort(array,new Comparator<Entry<String, Integer>>() {
@Override
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
return o2.getKey().compareTo(o1.getKey());
}
});
for (Entry<String, Integer> entry : array) {
System.out.println(entry.getKey());
}