zoukankan      html  css  js  c++  java
  • 对map集合按照value从大到小进行排序

    概述:

     基本特点:

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

    子类:

        |--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;

    }

  • 相关阅读:
    0593. Valid Square (M)
    0832. Flipping an Image (E)
    1026. Maximum Difference Between Node and Ancestor (M)
    0563. Binary Tree Tilt (E)
    0445. Add Two Numbers II (M)
    1283. Find the Smallest Divisor Given a Threshold (M)
    C Primer Plus note9
    C Primer Plus note8
    C Primer Plus note7
    C Primer Plus note6
  • 原文地址:https://www.cnblogs.com/liwei09k1/p/7722802.html
Copyright © 2011-2022 走看看