类PriorityQueue<E>

来源:互联网 发布:c 与java socket 编辑:程序博客网 时间:2024/06/06 00:11

1、继承关系

  • java.lang.Object
    • java.util.AbstractCollection<E>
      • java.util.AbstractQueue<E>
        • java.util.PriorityQueue<E>

2、没有任何优先权的队列是基于头优先的,优先权队列中的元素排列顺序为自然顺序,或者根据队列创建时提供的比较器而定顺序,这都取决于队列在创建时使用的是哪个构造器。优先权队列不允许null元素。依赖于自然排序的优先权队列也不允许非可比元素的插入(这样做可能会导致ClassCastException)。

3、对头和队列中指定排序关系不大,如果多个元素都绑定了最小值,且对头为其中之一--那么这绑定会随之瓦解。队列检索了操作:poll,remove,peek,element,获取的都是对头元素。

4、优先级队列是不受限制的,但它本身有一个数组大小的容量来存储数据。它的大小通常最小为队列大小。当有元素添加到队列中时,它的容量会自动增长,增长策略细节没有指定。

5、该类实现了所有的集合和迭代的方法。迭代器提供的方法 iterator() 并未指定按照某一特定顺序来遍历元素,如果你需要按序遍历,考虑使用Arrays.sort(pq.toArray())。

6、注意该实现是非同步的。如果有线程修改队列,多个线程是无法使用队列中的元素的。取而代之,使用线程安全类PriorityBlockingQueue 。

7、实现注意:入队出队方法(offer,poll,remove,add)的时间复杂度为:O(log(n)) ;remove和contains方法为线性时间;遍历方法为常量时间,peek,element,size。

8、方法概况:

Modifier and TypeMethod and Descriptionbooleanadd(E e)

Inserts the specified element into this priority queue.
voidclear()
Removes all of the elements from this priority queue.
Comparator<? super E>comparator()
Returns the comparator used to order the elements in this queue, or null if this queue is sorted according to the natural ordering of its elements.
booleancontains(Object o)
Returns true if this queue contains the specified element.
Iterator<E>iterator()
Returns an iterator over the elements in this queue.
booleanoffer(E e)
Inserts the specified element into this priority queue.
Epeek()
Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
Epoll()
Retrieves and removes the head of this queue, or returns null if this queue is empty.
booleanremove(Object o)
Removes a single instance of the specified element from this queue, if it is present.
intsize()
Returns the number of elements in this collection.
Object[]toArray()
Returns an array containing all of the elements in this queue.
<T> T[]toArray(T[] a)
Returns an array containing all of the elements in this queue; the runtime type of the returned array is that of the specified array.

  • Methods inherited from class java.util.AbstractQueue

    addAll, element, remove
  • Methods inherited from class java.util.AbstractCollection

    containsAll, isEmpty, removeAll, retainAll, toString
  • Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Methods inherited from interface java.util.Collection

    containsAll, equals, hashCode, isEmpty, removeAll, retainAll

0 0