java 如何在java中获得比较器的倒数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12345451/
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-10-31 08:30:30  来源:igfitidea点击:

How to get inverse of a comparator in java

javagenericsheappriority-queuecomparator

提问by Abhinav Batra

In a method I receive a generic object E extends Comparable<E>as an argument. Now i want to create two priority queues.One which uses the comparatorused by E and other queue which uses the opposite of comparatorused by E(i.e. if E uses '<' then second queue must use '>='). Please hep me how to create two such queues.

在一个方法中,我收到一个泛型object E extends Comparable<E>作为参数。现在我想创建两个优先级comparator队列。一个使用 E 使用的队列,另一个使用 E 使用的相反的队列comparator(即,如果 E 使用“<”,则第二个队列必须使用“>=”)。请教我如何创建两个这样的队列。

queue2=new PriorityQueue<E>(0,Collections.reverseOrder(e));

I am getting the error that reverseOrderis not applicable.

我收到reverseOrder不适用的错误。

please help

请帮忙

回答by casablanca

回答by Bill

Your object Eextends java.lang.Comparable,but it is not a java.util.Comparator.

您的对象E扩展java.lang.Comparable,但它不是java.util.Comparator.

Create your first queue w/o a Comparator and you'll get the ordering in your compareTofunction, then create a java.util.Comparatorthat does the comparison in reverse (just call a.compareTo(b) and then negate the result) and create your second queue with that comparator.

使用 Comparator 创建您的第一个队列,您将获得compareTo函数中的排序,然后创建一个java.util.Comparator反向比较的队列(只需调用 a.compareTo(b) 然后否定结果)并使用它创建您的第二个队列比较器。

回答by Stefan Birkner

The single argument of Collections.reverseOrderis a Comparator and not a Collection. For your code simply use reverseOrder without an argument. You have to use a non-zero inital size, too. The following code will work.

Collections.reverseOrder的单个参数是 Comparator 而不是 Collection。对于您的代码,只需使用不带参数的 reverseOrder。您也必须使用非零初始大小。以下代码将起作用。

queue2=new PriorityQueue<E>(1, Collections.reverseOrder());

回答by Amit Deshpande

Below Program depicts how to do it.

下面的程序描述了如何做到这一点。

I have StringLengthComparatorwhich compares based on string length. Using Collections.reverseOrderI have created queue which is reverse ordered and another queue which is ordered correctly.

我有StringLengthComparator基于字符串长度的比较。使用Collections.reverseOrder我创建了反向排序的队列和另一个正确排序的队列。

import java.util.Collections;
import java.util.Comparator;
import java.util.PriorityQueue;

public class TestReverseorder {
public static void main(String[] args) {
    Comparator<String> comparator = new TestReverseorder().new StringLengthComparator();
    PriorityQueue<String> reverse = new PriorityQueue<String>(10,
            Collections.reverseOrder(comparator));
    PriorityQueue<String> queue = new PriorityQueue<String>(10,comparator);
    queue.add("1");
    queue.add("12");
    queue.add("123");

    reverse.add("1");
    reverse.add("12");
    reverse.add("123");

    while (!queue.isEmpty()) {
        System.out.println(queue.poll());
    }

    while (!reverse.isEmpty()) {
        System.out.println(reverse.poll());
    }


}

public class StringLengthComparator implements Comparator<String> {
    @Override
    public int compare(String x, String y) {
        // Assume neither string is null. Real code should
        // probably be more robust
        if (x.length() < y.length()) {
            return -1;
        }
        if (x.length() > y.length()) {
            return 1;
        }
        return 0;
    }
}
}

It will print output

它会打印输出

Normal Order:
1
12
123
Reverse Order:
123
12
1