PriorityQueue throwing class cast exception












1















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.










share|improve this question




















  • 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 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






  • 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








  • 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
















1















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.










share|improve this question




















  • 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 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






  • 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








  • 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














1












1








1








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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





    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






  • 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








  • 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





    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 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






  • 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








  • 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












1 Answer
1






active

oldest

votes


















1
















  1. Implement Comparable interface and override ComapareTo inside Vertex 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.



  2. 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 the ClassCastException 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






share|improve this answer























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


    }
    });














    draft saved

    draft discarded


















    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









    1
















    1. Implement Comparable interface and override ComapareTo inside Vertex 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.



    2. 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 the ClassCastException 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






    share|improve this answer




























      1
















      1. Implement Comparable interface and override ComapareTo inside Vertex 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.



      2. 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 the ClassCastException 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






      share|improve this answer


























        1












        1








        1









        1. Implement Comparable interface and override ComapareTo inside Vertex 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.



        2. 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 the ClassCastException 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






        share|improve this answer















        1. Implement Comparable interface and override ComapareTo inside Vertex 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.



        2. 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 the ClassCastException 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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 15:00









        CodeBusker_JEPCodeBusker_JEP

        57027




        57027
































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas