【问题标题】:sort a hashmap by the Integer Value desc按整数值 desc 对哈希图进行排序
【发布时间】:2016-03-26 16:37:21
【问题描述】:

如何按整数值对hashmap 进行排序,我找到的答案之一是here

Evgeniy Dorofeev 写的,他的回答是这样的

HashMap<String, Integer> map = new HashMap<String, Integer>();
    map.put("a", 4);
    map.put("c", 6);
    map.put("b", 2);
    Object[] a = map.entrySet().toArray();
    Arrays.sort(a, new Comparator() {
        public int compare(Object o1, Object o2) {
            return ((Map.Entry<String, Integer>) o2).getValue().compareTo(
                    ((Map.Entry<String, Integer>) o1).getValue());
        }
    });
    for (Object e : a) {
        System.out.println(((Map.Entry<String, Integer>) e).getKey() + " : "
                + ((Map.Entry<String, Integer>) e).getValue());
    }

输出

c : 6
a : 4
b : 2

我的问题是排序如何变成 Desc ?如果我想对HashMap Asc 进行排序,我该怎么做??

最后一个问题是:排序后如何获得第一个元素?

【问题讨论】:

  • 您可以通过在compare 方法中切换o2o1 来反转顺序 - 获取第一个元素只是a[0],然后使用与for循环中相同的逻辑获取值和密钥!?
  • 不,我的问题是关于我在问题中指定的用户编写的代码:)

标签: java sorting


【解决方案1】:

用于逆序开关o2o1。要获取第一个元素,只需访问索引 0 处的数组:

Map<String, Integer> map = new HashMap<>();
map.put("a", 4);
map.put("c", 6);
map.put("b", 2);
Object[] a = map.entrySet().toArray();
Arrays.sort(a, new Comparator() {
    public int compare(Object o1, Object o2) {
        return ((Map.Entry<String, Integer>) o1).getValue().compareTo(
               ((Map.Entry<String, Integer>) o2).getValue());
    }
});
for (Object e : a) {
    System.out.println(((Map.Entry<String, Integer>) e).getKey() + " : "
                     + ((Map.Entry<String, Integer>) e).getValue());
}        

System.out.println("first element is " + ((Map.Entry<String, Integer>) a[0]).getKey() + " : "
        + ((Map.Entry<String, Integer>) a[0]).getValue());        

打印出来的

b : 2
一:4
c : 6
第一个元素是 b : 2

如果您可以访问 lambda 表达式,您可以使用这些来简化排序:

Arrays.sort(a, (o1, o2) -> 
   ((Map.Entry<String, Integer>) o1).getValue().compareTo(((Map.Entry<String, Integer>) o2).getValue()));

【讨论】:

    【解决方案2】:

    在 Java 8 中,您可以执行以下操作:

    System.out.println(map.entrySet().stream().sorted((o1, o2) -> {
            return o2.getValue().compareTo(o1.getValue());
        }).findFirst());//would return entry boxed into optional which you can unbox.
    

    【讨论】:

    • 感谢您的回答:)
    • 没有问题。它简洁易读。您不必从对象到条目进行多次转换,反之亦然。
    【解决方案3】:

    首先,回答你的问题:只需反转compare方法的结果,将ASC更改为DESC。

    HashMap<String, Integer> map = new HashMap<String, Integer>();
    map.put("a", 4);
    map.put("c", 6);
    map.put("b", 2);
    Object[] a = map.entrySet().toArray();
    Arrays.sort(a, new Comparator() {
        public int compare(Object o1, Object o2) {
            // just reverse the result of the comparison 
            return -((Map.Entry<String, Integer>) o2).getValue().compareTo(
                    ((Map.Entry<String, Integer>) o1).getValue());
        }
    });
    for (Object e : a) {
        System.out.println(((Map.Entry<String, Integer>) e).getKey() + " : "
                + ((Map.Entry<String, Integer>) e).getValue());
    }
    

    但如果您需要使用已排序的Map,我建议您使用TreeMap 的实例来自行处理排序。

    【讨论】:

    • 非常感谢@Orlangure
    猜你喜欢
    • 2014-01-30
    • 2011-10-25
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 2020-04-05
    • 1970-01-01
    • 2011-03-10
    相关资源
    最近更新 更多