2

I am trying to use a priority queue (java.util.PriorityQueue) on a custom class. I understand that the PriorityQueue class uses the Comparable interface, so I implemented it in my custom class:

public class State implements Comparable<State> {
    public int val;

    public State(){
        this.val = 0;
    }

    public int compareTo(State other){
        return this.val - other.val;
    }
}

And to use the queue, I did:

PriorityQueue<State> q = new PriorityQueue<State>();
q.add(myState1);
q.add(myState2);
// etc.

It compiles correctly, but I get this exception during runtime:

Exception in thread "main" java.lang.ClassCastException: State cannot be cast to
java.lang.Comparable
    at java.util.PriorityQueue.siftUpComparable(Unknown Source)
    at java.util.PriorityQueue.siftUp(Unknown Source)
    at java.util.PriorityQueue.offer(Unknown Source)
    at java.util.PriorityQueue.add(Unknown Source)
    at MapTable.search(MapTable.java:308)
    at Map.main(Map.java:67)

What am I doing wrong? As far as I can remember, this is how I would implement comparable. Thanks for all answers.

5
  • 5
    Are you sure the State in the Comparable<State> is the right one, and not java.lang.Thread.State or javax.swing.plaf.nimbus.State for example? Commented Sep 9, 2012 at 20:16
  • 1
    @amit wouldn't the exception print the qualified name of the class in that case? It appears that the issue is probably that he forgot to recompile State. Commented Sep 9, 2012 at 20:28
  • @oldrinb You are right, that did it! I assumed it would compile, since I use the object in several places before, but I guess it didn't for some reason. But compiling it separately did the trick. Thanks. Commented Sep 9, 2012 at 20:32
  • @oldrinb: It is an answer. post it. Commented Sep 9, 2012 at 20:36
  • @Richard done :-) Commented Sep 9, 2012 at 20:45

2 Answers 2

3

amit originally suggested in his comment a very wise piece of advice; essentially, make sure you're not referring to another class named State which is hiding your own State from view, e.g. java.lang.Thread.State and javax.swing.plaf.nimbus.State. This is often the cause of many bugs, so make sure you remember this.

Unfortunately, it's not the case here. As I pointed out in a comment, the exception would print the qualified name of whatever other State as a part of the exception message in that case. Since the qualified name was State, it makes sense that you likely forgot to recompile State. :-)

Sign up to request clarification or add additional context in comments.

1 Comment

Can you explain this in detail, I am getting the same error but couldn't understand your answer.
1

Your code is working correctly in my Eclipse ide. Maybe you have imported a class State from a different package?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.