Java 优先级队列和类似的接口

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

Java priority queues and comparable interface

javapriority-queuecomparable

提问by JohnnyHunter

I've just been learning about priority queues and thought I'd try how it behaves with comparable interface.

我刚刚在学习优先级队列,并认为我会尝试使用可比较的接口来表现它的行为。

Code Snippet:

代码片段:

import java.util.PriorityQueue;

class kinga implements Comparable<Double> {
    double time=909.909;
    double d;

    public kinga(double a) {  
        this.d=a;
    }

    public int compareTo(Double d) {
        return Double.compare(d, time);
    }

    public static void main(String arg[]) {
        PriorityQueue<kinga> r=new PriorityQueue<kinga>();

        r.add( new kinga(4545.45));
        r.add( new kinga(45.4));
        r.add( new kinga(1235.45));

        System.out.println(r.poll()+" "+r.poll()+" "+r.poll());
    }
}

It compiles but gives me Exception in thread "main"java.lang.ClassCastException: kinga cannot be cast to java.lang.Double.

它编译但在线程 "main" 中给了我异常java.lang.ClassCastException: kinga cannot be cast to java.lang.Double

What is wrong here. Can somebody tell me how comparable and priority queues work?

这里有什么问题。有人能告诉我可比队列和优先队列是如何工作的吗?

采纳答案by Katona

kingashould be comparable with kinga, not Double, so:

kinga应该与 相比kinga,而不是Double,所以:

class kinga implements Comparable<kinga>

which means your compareTomethod has to be changed to this:

这意味着您的compareTo方法必须更改为:

public int compareTo(kinga o) {
    return Double.compare(o.d, d);
}

回答by sanbhat

PriorityQueue<kinga>will expect Comparable<kinga>in the addmethod. Passing a Comparable<Dobule>instead, is throwing ClassCastException

PriorityQueue<kinga>将期望Comparable<kinga>add方法中。传递一个Comparable<Dobule>而不是,正在抛出ClassCastException

回答by JB Nizet

class kinga implements Comparable<Double>

That doesn't make sense. Although your class will compare fine with Double, Double is unaware of that, and won't compare fine with instances of kinga, which will break the Comparable contract. And since a kinga can't compare with another kinga, you can't use a PriorityQueue<kinga>.

那没有意义。尽管您的类会与 Double 进行比较,但 Double 不知道这一点,并且不会与 kinga 的实例进行比较,这会破坏 Comparable 契约。由于一个kinga 无法与另一个kinga 进行比较,因此您不能使用PriorityQueue<kinga>.

It should be

它应该是

class Kinga implements Comparable<Kinga>

(note the upper-case, to respect the Java naming conventions), which means: Kinga instances are comparable together.

(注意大写,以尊重 Java 命名约定),这意味着:Kinga 实例可以一起比较。

The compareTo method should be

compareTo 方法应该是

@Override
public int compareTo(Kinga other) {
    return Double.compare(this.d, other.d);
}

which means: I'm bigger than another Kinga if my dis bigger than the other Kinga's d.

这意味着:如果 my 比另一个 Kinga 大,我d就比另一个 Kinga大d

回答by Aniket Thakur

Can somebody tell me how comparable and priority queues work?

First get the differencebetween Comparable and Comparator interfaces.

先搞清楚Comparable 和 Comparator 接口的区别

Now for your question you can do something like below

现在对于您的问题,您可以执行以下操作

First create a Comparatorfor Kinga

首先为 Kinga创建一个Comparator

class comparableKinga implements Comparator<kinga> {

@Override
public int compare(kinga o1, kinga o2) {
    return Double.compare(o1.getD(),o2.getD());
}
}

Then create your priority queue with this Comparator in the constructor

然后在构造函数中使用此 Comparator创建您的优先级队列

class kinga {

double d;

public kinga(double a) {
    this.d = a;
}

public double getD() {
    return this.d;
}

@Override
public String toString() {
    return "kinga{" +
            "d=" + d +
            '}';
}

public static void main(String arg[]) {
    PriorityQueue<kinga> r = new PriorityQueue<kinga>(11,new comparableKinga());


    r.add(new kinga(4545.45));
    r.add(new kinga(45.4));
    r.add(new kinga(1235.45));

    System.out.println(r.poll() + " " + r.poll() + " " + r.poll());
}
}

Outputis as expected

输出符合预期

kinga{d=45.4} kinga{d=1235.45} kinga{d=4545.45}