Sorting PriorityQueue

前端 未结 3 763
难免孤独
难免孤独 2020-12-18 08:50

I am having a problem with PriorityQueues, as I am lead to believe it orders on priority however I am not sure what the priority is (I mean what the value is and where it co

3条回答
  •  清歌不尽
    2020-12-18 09:24

    Issue is Iterator.As Documented in Java doc of PriorityQueue#iterator

    Returns an iterator over the elements in this queue. The iterator does not return the elements in any particular order.

    As toString uses iterator it will not get printed in order. Or if you use loop based on iterator then also it will be in order.

    And in the Java doc of PriorityQueue

    The queue retrieval operations poll, remove, peek, and element access the element at the head of the queue.

    To get results in order you will have to use one of these methods.

提交回复
热议问题