PriorityQueue throwing class cast exception
PriorityQueue add method throws class cast exception (MyVertex cannot be cast to java.lang.Comparable) on executing.
Some Object of type MyVertex are inserted correctly, and some throws exception, couldn't manage to find the differences between them.
See the attached lines of code in java:
PriorityQueue<Vertex> pq = new PriorityQueue<>();
for (Edge edge : vertex.getEdges()) {
pq.add(edge.getTo());
}
expected: The method pq.add() should not throw an exception.
java priority-queue classcastexception comparable
|
show 5 more comments
PriorityQueue add method throws class cast exception (MyVertex cannot be cast to java.lang.Comparable) on executing.
Some Object of type MyVertex are inserted correctly, and some throws exception, couldn't manage to find the differences between them.
See the attached lines of code in java:
PriorityQueue<Vertex> pq = new PriorityQueue<>();
for (Edge edge : vertex.getEdges()) {
pq.add(edge.getTo());
}
expected: The method pq.add() should not throw an exception.
java priority-queue classcastexception comparable
3
is MyVertex implements Comparable?
– wardziniak
Jan 2 at 14:41
3
SeePriorityQueue.add()
: "Throws: ClassCastException - if the specified element cannot be compared with elements currently in this priority queue according to the priority queue's ordering" - show us yourMyVertex.compareTo
-implementation, seems you've got a bug in there.
– Hulk
Jan 2 at 14:44
Side note: you forgot the diamond innew PriorityQueue<>();
- please don't use raw types.
– Hulk
Jan 2 at 14:59
1
@Hulk, First element will be added toPriorityQueue
even if it doesn't implementComparable
since there are no other elements to compare to.
– Ivan
Jan 2 at 15:12
1
@LiranSimanTov Please edit your question to include a Minimal, Complete, and Verifiable example including your classes, which can be compiled and tested by others, which throws an exception when you try to add a new entry to the queue.
– Progman
Jan 2 at 19:24
|
show 5 more comments
PriorityQueue add method throws class cast exception (MyVertex cannot be cast to java.lang.Comparable) on executing.
Some Object of type MyVertex are inserted correctly, and some throws exception, couldn't manage to find the differences between them.
See the attached lines of code in java:
PriorityQueue<Vertex> pq = new PriorityQueue<>();
for (Edge edge : vertex.getEdges()) {
pq.add(edge.getTo());
}
expected: The method pq.add() should not throw an exception.
java priority-queue classcastexception comparable
PriorityQueue add method throws class cast exception (MyVertex cannot be cast to java.lang.Comparable) on executing.
Some Object of type MyVertex are inserted correctly, and some throws exception, couldn't manage to find the differences between them.
See the attached lines of code in java:
PriorityQueue<Vertex> pq = new PriorityQueue<>();
for (Edge edge : vertex.getEdges()) {
pq.add(edge.getTo());
}
expected: The method pq.add() should not throw an exception.
java priority-queue classcastexception comparable
java priority-queue classcastexception comparable
edited Jan 2 at 16:43
Liran Siman Tov
asked Jan 2 at 14:40
Liran Siman TovLiran Siman Tov
215
215
3
is MyVertex implements Comparable?
– wardziniak
Jan 2 at 14:41
3
SeePriorityQueue.add()
: "Throws: ClassCastException - if the specified element cannot be compared with elements currently in this priority queue according to the priority queue's ordering" - show us yourMyVertex.compareTo
-implementation, seems you've got a bug in there.
– Hulk
Jan 2 at 14:44
Side note: you forgot the diamond innew PriorityQueue<>();
- please don't use raw types.
– Hulk
Jan 2 at 14:59
1
@Hulk, First element will be added toPriorityQueue
even if it doesn't implementComparable
since there are no other elements to compare to.
– Ivan
Jan 2 at 15:12
1
@LiranSimanTov Please edit your question to include a Minimal, Complete, and Verifiable example including your classes, which can be compiled and tested by others, which throws an exception when you try to add a new entry to the queue.
– Progman
Jan 2 at 19:24
|
show 5 more comments
3
is MyVertex implements Comparable?
– wardziniak
Jan 2 at 14:41
3
SeePriorityQueue.add()
: "Throws: ClassCastException - if the specified element cannot be compared with elements currently in this priority queue according to the priority queue's ordering" - show us yourMyVertex.compareTo
-implementation, seems you've got a bug in there.
– Hulk
Jan 2 at 14:44
Side note: you forgot the diamond innew PriorityQueue<>();
- please don't use raw types.
– Hulk
Jan 2 at 14:59
1
@Hulk, First element will be added toPriorityQueue
even if it doesn't implementComparable
since there are no other elements to compare to.
– Ivan
Jan 2 at 15:12
1
@LiranSimanTov Please edit your question to include a Minimal, Complete, and Verifiable example including your classes, which can be compiled and tested by others, which throws an exception when you try to add a new entry to the queue.
– Progman
Jan 2 at 19:24
3
3
is MyVertex implements Comparable?
– wardziniak
Jan 2 at 14:41
is MyVertex implements Comparable?
– wardziniak
Jan 2 at 14:41
3
3
See
PriorityQueue.add()
: "Throws: ClassCastException - if the specified element cannot be compared with elements currently in this priority queue according to the priority queue's ordering" - show us your MyVertex.compareTo
-implementation, seems you've got a bug in there.– Hulk
Jan 2 at 14:44
See
PriorityQueue.add()
: "Throws: ClassCastException - if the specified element cannot be compared with elements currently in this priority queue according to the priority queue's ordering" - show us your MyVertex.compareTo
-implementation, seems you've got a bug in there.– Hulk
Jan 2 at 14:44
Side note: you forgot the diamond in
new PriorityQueue<>();
- please don't use raw types.– Hulk
Jan 2 at 14:59
Side note: you forgot the diamond in
new PriorityQueue<>();
- please don't use raw types.– Hulk
Jan 2 at 14:59
1
1
@Hulk, First element will be added to
PriorityQueue
even if it doesn't implement Comparable
since there are no other elements to compare to.– Ivan
Jan 2 at 15:12
@Hulk, First element will be added to
PriorityQueue
even if it doesn't implement Comparable
since there are no other elements to compare to.– Ivan
Jan 2 at 15:12
1
1
@LiranSimanTov Please edit your question to include a Minimal, Complete, and Verifiable example including your classes, which can be compiled and tested by others, which throws an exception when you try to add a new entry to the queue.
– Progman
Jan 2 at 19:24
@LiranSimanTov Please edit your question to include a Minimal, Complete, and Verifiable example including your classes, which can be compiled and tested by others, which throws an exception when you try to add a new entry to the queue.
– Progman
Jan 2 at 19:24
|
show 5 more comments
1 Answer
1
active
oldest
votes
Implement
Comparable
interface and overrideComapareTo
insideVertex
class to satisfy the priority queue's ordering.
As the java doc says -
add
throws ClassCastException if the specified element cannot be compared with elements currently in this priority queue according to the priority queue's ordering.
As you say it was working before, its because the add call will not throw the ClassCastException when the priorityQueues's size is 0 (when first element is added). To test that, before the call to the for loop print the size of
vertex.getEdges().size()
. If the size appears to be greater than 0, thats when theClassCastException
will be thrown.
When size is not zero a sift-up operation is invoked as the underlying data_structure in the priority queue here is a heap.
The exception that you see is triggered from sift-up
, as sift-up internally requires your elements to implement Comparable
. And sift-up
is called when the priority queue size is not 0, or a subsequent element is added.
Note:
Heap should satisfy the heap property: if P is a parent node of C,
then the key (the value) of P is either greater than or equal to (in
a max heap) or less than or equal to (in a min heap) the key of C.
sift-up: move a node up in the tree, as long as needed; used to
restore heap condition after insertion. Called "sift" because node
moves up the tree until it reaches the correct level, as in a sieve.
Heap_data_structure
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54008273%2fpriorityqueue-throwing-class-cast-exception%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Implement
Comparable
interface and overrideComapareTo
insideVertex
class to satisfy the priority queue's ordering.
As the java doc says -
add
throws ClassCastException if the specified element cannot be compared with elements currently in this priority queue according to the priority queue's ordering.
As you say it was working before, its because the add call will not throw the ClassCastException when the priorityQueues's size is 0 (when first element is added). To test that, before the call to the for loop print the size of
vertex.getEdges().size()
. If the size appears to be greater than 0, thats when theClassCastException
will be thrown.
When size is not zero a sift-up operation is invoked as the underlying data_structure in the priority queue here is a heap.
The exception that you see is triggered from sift-up
, as sift-up internally requires your elements to implement Comparable
. And sift-up
is called when the priority queue size is not 0, or a subsequent element is added.
Note:
Heap should satisfy the heap property: if P is a parent node of C,
then the key (the value) of P is either greater than or equal to (in
a max heap) or less than or equal to (in a min heap) the key of C.
sift-up: move a node up in the tree, as long as needed; used to
restore heap condition after insertion. Called "sift" because node
moves up the tree until it reaches the correct level, as in a sieve.
Heap_data_structure
add a comment |
Implement
Comparable
interface and overrideComapareTo
insideVertex
class to satisfy the priority queue's ordering.
As the java doc says -
add
throws ClassCastException if the specified element cannot be compared with elements currently in this priority queue according to the priority queue's ordering.
As you say it was working before, its because the add call will not throw the ClassCastException when the priorityQueues's size is 0 (when first element is added). To test that, before the call to the for loop print the size of
vertex.getEdges().size()
. If the size appears to be greater than 0, thats when theClassCastException
will be thrown.
When size is not zero a sift-up operation is invoked as the underlying data_structure in the priority queue here is a heap.
The exception that you see is triggered from sift-up
, as sift-up internally requires your elements to implement Comparable
. And sift-up
is called when the priority queue size is not 0, or a subsequent element is added.
Note:
Heap should satisfy the heap property: if P is a parent node of C,
then the key (the value) of P is either greater than or equal to (in
a max heap) or less than or equal to (in a min heap) the key of C.
sift-up: move a node up in the tree, as long as needed; used to
restore heap condition after insertion. Called "sift" because node
moves up the tree until it reaches the correct level, as in a sieve.
Heap_data_structure
add a comment |
Implement
Comparable
interface and overrideComapareTo
insideVertex
class to satisfy the priority queue's ordering.
As the java doc says -
add
throws ClassCastException if the specified element cannot be compared with elements currently in this priority queue according to the priority queue's ordering.
As you say it was working before, its because the add call will not throw the ClassCastException when the priorityQueues's size is 0 (when first element is added). To test that, before the call to the for loop print the size of
vertex.getEdges().size()
. If the size appears to be greater than 0, thats when theClassCastException
will be thrown.
When size is not zero a sift-up operation is invoked as the underlying data_structure in the priority queue here is a heap.
The exception that you see is triggered from sift-up
, as sift-up internally requires your elements to implement Comparable
. And sift-up
is called when the priority queue size is not 0, or a subsequent element is added.
Note:
Heap should satisfy the heap property: if P is a parent node of C,
then the key (the value) of P is either greater than or equal to (in
a max heap) or less than or equal to (in a min heap) the key of C.
sift-up: move a node up in the tree, as long as needed; used to
restore heap condition after insertion. Called "sift" because node
moves up the tree until it reaches the correct level, as in a sieve.
Heap_data_structure
Implement
Comparable
interface and overrideComapareTo
insideVertex
class to satisfy the priority queue's ordering.
As the java doc says -
add
throws ClassCastException if the specified element cannot be compared with elements currently in this priority queue according to the priority queue's ordering.
As you say it was working before, its because the add call will not throw the ClassCastException when the priorityQueues's size is 0 (when first element is added). To test that, before the call to the for loop print the size of
vertex.getEdges().size()
. If the size appears to be greater than 0, thats when theClassCastException
will be thrown.
When size is not zero a sift-up operation is invoked as the underlying data_structure in the priority queue here is a heap.
The exception that you see is triggered from sift-up
, as sift-up internally requires your elements to implement Comparable
. And sift-up
is called when the priority queue size is not 0, or a subsequent element is added.
Note:
Heap should satisfy the heap property: if P is a parent node of C,
then the key (the value) of P is either greater than or equal to (in
a max heap) or less than or equal to (in a min heap) the key of C.
sift-up: move a node up in the tree, as long as needed; used to
restore heap condition after insertion. Called "sift" because node
moves up the tree until it reaches the correct level, as in a sieve.
Heap_data_structure
answered Jan 3 at 15:00
CodeBusker_JEPCodeBusker_JEP
57027
57027
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54008273%2fpriorityqueue-throwing-class-cast-exception%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
is MyVertex implements Comparable?
– wardziniak
Jan 2 at 14:41
3
See
PriorityQueue.add()
: "Throws: ClassCastException - if the specified element cannot be compared with elements currently in this priority queue according to the priority queue's ordering" - show us yourMyVertex.compareTo
-implementation, seems you've got a bug in there.– Hulk
Jan 2 at 14:44
Side note: you forgot the diamond in
new PriorityQueue<>();
- please don't use raw types.– Hulk
Jan 2 at 14:59
1
@Hulk, First element will be added to
PriorityQueue
even if it doesn't implementComparable
since there are no other elements to compare to.– Ivan
Jan 2 at 15:12
1
@LiranSimanTov Please edit your question to include a Minimal, Complete, and Verifiable example including your classes, which can be compiled and tested by others, which throws an exception when you try to add a new entry to the queue.
– Progman
Jan 2 at 19:24