map完成按value升序排序。本站提示廣大學習愛好者:(map完成按value升序排序)文章只能為提供參考,不一定能成為您想要的結果。以下是map完成按value升序排序正文
/**
* @param h
* @return
* 完成對map依照value升序排序
*/
@SuppressWarnings("unchecked")
public static Map.Entry[] getSortedHashtableByValue(Map h) {
Set set = h.entrySet();
Map.Entry[] entries = (Map.Entry[]) set.toArray(new Map.Entry[set
.size()]);
Arrays.sort(entries, new Comparator() {
public int compare(Object arg0, Object arg1) {
Long key1 = Long.valueOf(((Map.Entry) arg0).getValue().toString());
Long key2 = Long.valueOf(((Map.Entry) arg1).getValue().toString());
return key1.compareTo(key2);
}
});
return entries;
}
/**
* @param h
* @return
* 完成對map依照key排序
*/
@SuppressWarnings("unchecked")
public static Map.Entry[] getSortedHashtableByKey(Map h) {
Set set = h.entrySet();
Map.Entry[] entries = (Map.Entry[]) set.toArray(new Map.Entry[set
.size()]);
Arrays.sort(entries, new Comparator() {
public int compare(Object arg0, Object arg1) {
Object key1 = ((Map.Entry) arg0).getKey();
Object key2 = ((Map.Entry) arg1).getKey();
return ((Comparable) key1).compareTo(key2);
}
});
return entries;
}