排序降序:Java Map

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18923167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:32:36  来源:igfitidea点击:

Sorting Descending order: Java Map

javasortinghashmap

提问by add-semi-colons

What I want to do is sort a map by value. I went over many questions that are available on the stackoverflow site and found out following solution that does what I want but missing a small thing.

我想做的是按值对地图进行排序。我浏览了 stackoverflow 站点上提供的许多问题,并找到了以下解决方案,该解决方案可以满足我的需求,但遗漏了一些小东西。

Link1: Sorting Map

Link1:排序图

But the issue I am running into is that by default this is sorted by ascending order by value. I want to order it by descending order:

但我遇到的问题是,默认情况下,这是按值升序排序的。我想按降序排序:

So what I did was I created a class that implements a comparator

所以我所做的是我创建了一个实现比较器的类

class MyComparator implements Comparator {
    Map map;
    public MyComparator(Map map) {
        this.map = map;
    }
    public int compare(Object o1, Object o2) {
        return ((Integer) map.get(o2)).compareTo((Integer) map.get(o1));
    }
}

And then I pass my map to the treemap,

然后我将我的地图传递给树状图,

MyComparator comp = new MyComparator(myMap);
Map<String, Integer> newMap = new TreeMap(comp);
newMap.putAll(myMap);

This seems like bad approach because I feel this is inefficient. Is there a way to change the solution in the link to do ordering on descending order by default.

这似乎是不好的方法,因为我觉得这是低效的。有没有办法更改链接中的解决方案以默认按降序排序。

回答by Nicole

To change the solution in the link to sort by descending order, just reverse the condition:

要将链接中的解决方案更改为按降序排序,只需反转条件:

...
// Note: this comparator imposes orderings that are inconsistent with equals.    
public int compare(String a, String b) {
    if (base.get(a) >= base.get(b)) {
        return 1; // For ascending, return -1;
    } else {
        return -1; // For ascending, return 1;
    } // returning 0 would merge keys
}
...

回答by Holger

You should use new TreeMap<>(Collections.reverseOrder());.

你应该使用new TreeMap<>(Collections.reverseOrder());.

Map<String, Integer> newMap = new TreeMap<>(Collections.reverseOrder());
newMap.putAll(myMap);

or to reverse an existing comparator like the value-comparator Collections.reverseOrder(comparator). It works like your approach swapping the two objects before invoking compare/compareTo.

或反转现有的比较器,如 value-comparator Collections.reverseOrder(comparator)。它的工作原理类似于您在调用compare/之前交换两个对象的方法compareTo

回答by OCarneiro

You could simply invert the return value of your compare method by adding a minus sign at the beginning:

您可以通过在开头添加一个减号来简单地反转比较方法的返回值:

return -((Integer) map.get(o2)).compareTo((Integer) map.get(o1));

回答by Nilesh Jadav

    TreeMap<Long,String> treeMap = new TreeMap<Long,String>();

    NavigableMap <Long, String> nmap = treeMap.descendingMap();

    Set<Long, String> set = nmap.entrySet();

    Iterator<Long, String> iterator = set.iterator();

now u can iterate over iterator and extract the value using iterator.hasNext() and iterator.next() methods ......

现在你可以迭代迭代器并使用 iterator.hasNext() 和 iterator.next() 方法提取值......

回答by Farman

This will work :

这将工作:

      TreeMap<Integer, Integer> reverseInteger=new TreeMap<>(new Comparator<Integer>() {

        @Override
        public int compare(Integer o1, Integer o2) {
            return o2>o1?1:o2==o1?0:-1;
        }
    });