Why does my PriorityBlockingQueue in Java not sort properly?

本小妞迷上赌 提交于 2019-12-02 04:14:26

I suspect you are trying to iterate the PriorityBlockingQueue and print the elements.

Note that a Priority Queue data structure (AKA heap) does not guarantee ordering - it guarantees that the head is minimal, but no order guarantee on any of the following nodes.

If you want your data maintained sorted - I suggest using something like ConcurrentSkipListSet (Note however it is a set - thus it does not allow duplicate entrees), or maintaining a sorted List.

If you want to get the sorted elements using a PriorityBlockingQueue - you should iteratively delete the head and output the new head - until the priority queue is exhausted. It will guarantee an ordered output.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!