📜  Java.util.PriorityQueue类

📅  最后修改于: 2020-11-14 06:20:45             🧑  作者: Mango


介绍

java.util.PriorityQueue类是一个基于优先级堆的无界优先级队列。以下是PriorityQueue的重点-

  • 根据使用的构造函数,优先级队列的元素根据其自然顺序进行排序,或通过在队列构建时提供的Comparator进行排序。

  • 优先级队列不允许空元素。

  • 依赖自然顺序的优先级队列也不允许插入不可比较的对象。

类声明

以下是java.util.PriorityQueue类的声明-

public class PriorityQueue
   extends AbstractQueue
   implements Serializable

参量

以下是java.util.PriorityQueue类的参数-

E-这是此集合中包含的元素的类型。

类的构造函数

Sr.No. Constructor & Description
1

PriorityQueue()

This creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering.

2

PriorityQueue(Collection c)

This creates a PriorityQueue containing the elements in the specified collection.

3

PriorityQueue(int initialCapacity)

This creates a PriorityQueue with the specified initial capacity that orders its elements according to their natural ordering.

4

PriorityQueue(int initialCapacity, Comparator comparator)

This creates a PriorityQueue with the specified initial capacity that orders its elements according to the specified comparator.

5

PriorityQueue(PriorityQueue c)

This creates a PriorityQueue containing the elements in the specified priority queue.

6

PriorityQueue(SortedSet c)

This creates a PriorityQueue containing the elements in the specified sorted set.

类方法

Sr.No. Method & Description
1 boolean add(E e)

This method inserts the specified element into this priority queue.

2 void clear()

This method removes all of the elements from this priority queue.

3 Comparator comparator()

This method 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.

4 boolean contains(Object o)

This method returns true if this queue contains the specified element.

5 Iterator iterator()

This method returns an iterator over the elements in this queue.

6 boolean offer(E e)

This method inserts the specified element into this priority queue.

7 E peek()

This method retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.

8 E poll()

This method retrieves and removes the head of this queue, or returns null if this queue is empty.

9 boolean remove(Object o)

This method removes a single instance of the specified element from this queue, if it is present.

10 int size()

This method returns the number of elements in this collection.

11 Object[] toArray()

This method returns an array containing all of the elements in this queue.

12 T[] toArray(T[] a)

This method returns an array containing all of the elements in this queue; the runtime type of the returned array is that of the specified array.

方法继承

此类从以下类继承方法-

  • java.util.AbstractQueue
  • java.util.AbstractCollection
  • java.util.Object
  • java.util.Collection