对map集合按照value从大到小进行排序

来源:互联网 发布:股票比价关系软件 编辑:程序博客网 时间:2024/05/20 19:19

概述:

 基本特点:

     该集合存储键值对,而且要保证键的惟一性

子类:

    |--HashTable 底层是哈希数据表结构,不可以使用Null作为键或者值;该集合线程是同步的

    |--hashMap   底层是哈希数据表结构,可以使用Null作为键或者值,该集合线程是不同步的

    |--treemap   底层是二叉树结构,线程不同步,可以对Map中的键值可以排序

   Map集合的两种取出方式(原理:将map集合转换成set,再使用迭代器)

1.传入map集合即可

 

public static Map sortByComparator(Map unsortMap){
List list = new LinkedList(unsortMap.entrySet()); 
// System.out.println("list:"+list);
Collections.sort(list, new Comparator() 
{
public int compare(Object o1, Object o2) 
{
return ((Comparable) ((Map.Entry) (o2)).getValue())
.compareTo(((Map.Entry) (o1)).getValue());
}
});
Map sortedMap = new LinkedHashMap();

for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry)it.next();
sortedMap.put(entry.getKey(), entry.getValue());

return sortedMap;

}

原创粉丝点击