public class PriorityQueue<E> extends AbstractQueue<E>
The least element of the specified ordering is stored at the head of the queue and the greatest element is stored at the tail of the queue.
A PriorityQueue is not synchronized. If multiple threads will have to access
it concurrently, use the PriorityBlockingQueue
.
Constructor and Description |
---|
PriorityQueue()
Constructs a priority queue with an initial capacity of 11 and natural
ordering.
|
PriorityQueue(Collection<? extends E> c)
Constructs a priority queue that contains the elements of a collection.
|
PriorityQueue(int initialCapacity)
Constructs a priority queue with the specified capacity and natural
ordering.
|
PriorityQueue(int initialCapacity,
Comparator<? super E> comparator)
Constructs a priority queue with the specified capacity and comparator.
|
PriorityQueue(PriorityQueue<? extends E> c)
Constructs a priority queue that contains the elements of another
priority queue.
|
PriorityQueue(SortedSet<? extends E> c)
Constructs a priority queue that contains the elements of a sorted set.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E o)
Adds the specified object to the priority queue.
|
void |
clear()
Removes all the elements of the priority queue.
|
Comparator<? super E> |
comparator()
Gets the comparator of the priority queue.
|
boolean |
contains(Object object)
Answers if there is an element in this queue equals to the object.
|
Iterator<E> |
iterator()
Gets the iterator of the priority queue, which will not return elements
in any specified ordering.
|
boolean |
offer(E o)
Inserts the element to the priority queue.
|
E |
peek()
Gets but does not remove the head of the queue.
|
E |
poll()
Gets and removes the head of the queue.
|
boolean |
remove(Object o)
Removes the specified object from the priority queue.
|
int |
size()
Gets the size of the priority queue.
|
Object[] |
toArray()
Returns all the elements in an array.
|
<T> T[] |
toArray(T[] array)
Returns all the elements in an array, and the type of the result array is
the type of the argument array.
|
addAll, element, remove
containsAll, isEmpty, removeAll, retainAll, toString
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
containsAll, equals, hashCode, isEmpty, removeAll, retainAll
public PriorityQueue()
public PriorityQueue(int initialCapacity)
initialCapacity
- the specified capacity.IllegalArgumentException
- if the initialCapacity is less than 1.public PriorityQueue(int initialCapacity, Comparator<? super E> comparator)
initialCapacity
- the specified capacity.comparator
- the specified comparator. If it is null, the natural ordering
will be used.IllegalArgumentException
- if the initialCapacity is less than 1.public PriorityQueue(Collection<? extends E> c)
c
- the collection whose elements will be added to the priority
queue to be constructed.ClassCastException
- if any of the elements in the collection are not comparable.NullPointerException
- if any of the elements in the collection are null.public PriorityQueue(PriorityQueue<? extends E> c)
c
- the priority queue whose elements will be added to the
priority queue to be constructed.public PriorityQueue(SortedSet<? extends E> c)
c
- the sorted set whose elements will be added to the priority
queue to be constructed.public Iterator<E> iterator()
iterator
in interface Iterable<E>
iterator
in interface Collection<E>
iterator
in class AbstractCollection<E>
public int size()
size
in interface Collection<E>
size
in class AbstractCollection<E>
public void clear()
clear
in interface Collection<E>
clear
in class AbstractQueue<E>
AbstractCollection.iterator()
,
AbstractCollection.isEmpty()
,
AbstractCollection.size()
public boolean offer(E o)
o
- the element to add to the priority queue.ClassCastException
- if the element cannot be compared with the elements in the
priority queue using the ordering of the priority queue.NullPointerException
- if o
is null
.public E poll()
public E peek()
public Comparator<? super E> comparator()
public boolean remove(Object o)
remove
in interface Collection<E>
remove
in class AbstractCollection<E>
o
- the object to be removed.public boolean add(E o)
add
in interface Collection<E>
add
in class AbstractQueue<E>
o
- the object to be added.ClassCastException
- if the element cannot be compared with the elements in the
priority queue using the ordering of the priority queue.NullPointerException
- if o
is null
.public boolean contains(Object object)
contains
in interface Collection<E>
contains
in class AbstractCollection<E>
object
- the object to search for.true
if object is an element of this Collection
, false
otherwise.AbstractCollection.contains(java.lang.Object)
public Object[] toArray()
toArray
in interface Collection<E>
toArray
in class AbstractCollection<E>
AbstractCollection.toArray()
public <T> T[] toArray(T[] array)
toArray
in interface Collection<E>
toArray
in class AbstractCollection<E>
T
- the type of elements in the arrayarray
- the array stores all the elements from the queue, if it has
enough space; otherwise, a new array of the same type and the
size of the queue will be usedArrayStoreException
- if the type of the argument array is not compatible with
every element in the queueNullPointerException
- if the argument array is nulljava.util.AbstractCollection#toArray(T[])