Java Priority Queues and Related Interface

I just found out about the priority queues and thought that I would try how it behaves with a 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 an exception in the stream "main" java.lang.ClassCastException: kinga cannot be cast to java.lang.Double .

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

+7
java priority-queue comparable
source share
4 answers

kinga should be comparable to kinga , not Double , therefore:

 class kinga implements Comparable<kinga> 

which means that your compareTo method should be changed to this:

 public int compareTo(kinga o) { return Double.compare(od, d); } 
+8
source share
 class kinga implements Comparable<Double> 

It does not make sense. Although your class will compare the penalty with Double, Double is not aware of this and will not compare the penalty with kinga instances, which would violate the Comparable contract. And since the king cannot compare with another kingdom, you cannot use PriorityQueue<kinga> .

It should be

 class Kinga implements Comparable<Kinga> 

(note uppercase to abide by the Java naming conventions), which means: Kinga instances are comparable.

The compareTo method must be

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

which means: I am larger than the other King, if my d larger than the other Kinga d .

+8
source share

PriorityQueue<kinga> expects Comparable<kinga> in the add method. Passing a Comparable<Dobule> instead, throws a ClassCastException

+2
source share
 Can somebody tell me how comparable and priority queues work? 

First get the difference between the Comparable and Comparator interfaces.

Now for your question you can do something like below

First create a Comparator for Kinga

 class comparableKinga implements Comparator<kinga> { @Override public int compare(kinga o1, kinga o2) { return Double.compare(o1.getD(),o2.getD()); } } 

Then create a priority queue using this comparator in the constructor

 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()); } } 

Exit as expected

 kinga{d=45.4} kinga{d=1235.45} kinga{d=4545.45} 
0
source share

All Articles