Sorting a hash map first according to key then value

喜欢而已 提交于 2019-12-13 07:30:51

问题


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.


回答1:


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.




回答2:


(I'm assuming this is something like Java). Sorted maps only deal with keys, not values. You probably want to just load the map content into a new TreeMap where each key is the original key and value combined somehow. Or, you can load into an List and sort that, or use a Set.



来源:https://stackoverflow.com/questions/26952639/sorting-a-hash-map-first-according-to-key-then-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!