首先根据键的值对值进行排序 [英] Sorting a hash map first according to key then value

查看:98
本文介绍了首先根据键的值对值进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  TreeMap< String,Integer>< p> dictionary = new TreeMap< String,Integer>(); 

例如,期望的输出如下所示:

  it  -  2 
- 2
- 2
- 2
是 - 2
最好 - 1
最差 - 1

我尝试了树形图,但只根据键进行排序。
所以基本上,我需要根据整数(从最高到最低)排序,然后根据字符串(首先A然后B到Z)排序。
我在网上搜索,看到我将不得不使用比较器,但我不知道如何使它工作?
任何指导都将被赞赏。

这是用于Java分配的。

解决方案



 

地图< String,Integer>地图; //填充
List< Map.Entry< String,Integer>> entries = new ArrayList<> (map.entrySet());
Collections.sort(entries,new Comparator< Map.Entry< String,Integer>>(){
public int compareTo(Map.Entry< String,Integer> a,Map.Entry< String,整数> b){
返回a.getValue()。equals(b.getValue())?a.getKey()。compareTo(b.getKey()):Integer.compareTo(b.getValue(), a.getValue());
}
});
for(Map.Entry< String,Integer> entry:entries)
System.out.println(entry.getKey()+ - + entry.getValue());

如果Java 8可用,它会变得更加整洁:

 映射< String,Integer>地图; //填充
map.entrySet()。stream()
.sorted((a,b) - > a.getValue()。equals(b.getValue())?a.getKey ).compareTo(b.getKey()):Integer.compareTo(b.getValue(),a.getValue()))
.map(e - > entry.getKey()+ - + entry .getValue())
.forEach(System.out :: println);

如果绝对需要地图,请将它们加载到 LinkedHashMap ,它保留顺序,因为它按照它们插入的顺序迭代它的条目。


I need to sort a hash map, first according to value then according to key.

TreeMap<String,Integer> dictionary = new TreeMap<String,Integer>();

For example, the desired output is something like:

it - 2
of - 2
the - 2
times - 2
was - 2
best - 1
worst - 1

I tried tree map, but that only sorts it according to key. So basically, I need to sort my first according to the Integer (Highest to Lowest) and then according to the String (First A then B till Z). I searched online and saw that I'm gonna have to use comparator but I'm not sure how to make it work? Any guidance will be appreciated.

This is for a Java assignment.

解决方案

The Map interface has no concept of "order", but you could sort the entries:

Map<String, Integer> map; // populated
List<Map.Entry<String, Integer>> entries = new ArrayList<> (map.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<String, Integer>>() {
    public int compareTo(Map.Entry<String, Integer> a, Map.Entry<String, Integer> b) {
        return a.getValue().equals(b.getValue()) ? a.getKey().compareTo(b.getKey()) : Integer.compareTo(b.getValue(), a.getValue());
    }
});
for (Map.Entry<String, Integer> entry : entries)
    System.out.println(entry.getKey() + " - " + entry.getValue());

If java 8 is available, it becomes a lot neater:

Map<String, Integer> map; // populated
map.entrySet().stream()
    .sorted( (a, b) -> a.getValue().equals(b.getValue()) ? a.getKey().compareTo(b.getKey()) : Integer.compareTo(b.getValue(), a.getValue()))
    .map(e -> entry.getKey() + " - " + entry.getValue())
    .forEach(System.out::println);

If absolutely need a map, load them into aLinkedHashMap, which "preserves order" in that it iterates over its entries in the same order they were inserted.

这篇关于首先根据键的值对值进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆