队列不自然排序

编程入门 行业动态 更新时间:2024-10-21 17:40:29
本文介绍了队列不自然排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可能重复: 为什么在Java中的PriorityQueue中会发生这种奇怪的顺序?/a>

Possible Duplicate: Why is this strange order happens in PriorityQueue in java?

请参见下面的代码:

public static void main(String[] args) { Queue<String> q = new PriorityQueue<String>(); q.offer("car"); q.offer("airplane"); q.offer("bicycle"); Iterator<String> i = q.iterator(); while(i.hasNext()) System.out.print(i.next() + " "); }

有人可以解释一下为什么输出

Can someone explain me why the output is

airplane car bicycle

代替

airplane bicycle car

?

自在API中以来,表示优先级队列的元素是根据其自然顺序进行排序的.

Since in the API it says that the elements of the priority queue are ordered according to their natural ordering.

推荐答案

PriorityQueue基于优先级堆.尽管未对元素进行排序,但此数据结构允许非常快地检索最小元素.将元素添加到PriorityQueue的速度比添加到基于树的TreeSet的速度快.由于未对元素进行排序,因此如API所述,迭代器不会以任何特定顺序返回元素".

PriorityQueue is based on a priority heap. This data structure allows to retrieve the least element very quickly though the elements are not sorted. Adding elements to PriorityQueue is faster than to TreeSet which is tree based. Since the elements are not sorted, the iterator, as API says, "does not return the elements in any particular order".

更多推荐

队列不自然排序

本文发布于:2023-10-14 22:04:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1492346.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:队列   不自然

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!