map实现按value升序排序
程序员文章站
2024-03-31 11:20:46
复制代码 代码如下: /** * @param h * @retu...
复制代码 代码如下:
/**
* @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;
}
下一篇: 简单了解Spring中的事务控制