public class PriorityQueue<E>
- Object
- AbstractCollection<E>
- AbstractQueue<E>
- PriorityQueue
ImplementsCollection<E>, Iterable<E>, Queue<E>
A PriorityQueue holds elements on a priority heap, which orders the elements according to their natural order or according to the comparator specified at construction time. If the queue uses natural ordering, only elements that are comparable are permitted to be inserted into the queue.
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 java.util.concurrent.PriorityBlockingQueue.
Constructors
public PriorityQueue() | Constructs a priority queue with an initial capacity of 11 and natural ordering. |
public PriorityQueue(int initialCapacity) | Constructs a priority queue with the specified capacity and natural ordering. |
public PriorityQueue(int initialCapacity, Comparator<? super E> comparator) | Constructs a priority queue with the specified capacity and comparator. |
public PriorityQueue(Collection<? extends E> c) | Constructs a priority queue that contains the elements of a collection. |
public PriorityQueue(PriorityQueue<? extends E> c) | Constructs a priority queue that contains the elements of another priority queue. |
public PriorityQueue(SortedSet<? extends E> c) | Constructs a priority queue that contains the elements of a sorted set. |
Methods
public Iterator<E> iterator() | Gets the iterator of the priority queue, which will not return elements in any specified ordering. |
public int size() | Gets the size of the priority queue. |
public void clear() | Removes all the elements of the priority queue. |
public boolean offer(E o) | Inserts the element to the priority queue. |
public E poll() | Gets and removes the head of the queue. |
public E peek() | Gets but does not remove the head of the queue. |
public Comparator<? super E> comparator() | Gets the comparator of the priority queue. |
public boolean remove(Object o) | Removes the specified object from the priority queue. |
public boolean add(E o) | Adds the specified object to the priority queue. |
public boolean contains(Object object) | Answers if there is an element in this queue equals to the object. |
public Object[] toArray() | Returns all the elements in an array. |
public <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. |
Inherited methods
From AbstractQueue
From Collection
Constructor details
PriorityQueue
public PriorityQueue()PriorityQueue
public PriorityQueue(int initialCapacity)Parameters
initialCapacityint- the specified capacity.
Throws
IllegalArgumentException- if the initialCapacity is less than 1.
PriorityQueue
public PriorityQueue(int initialCapacity, Comparator<? super E> comparator)Parameters
initialCapacityint- the specified capacity.
comparatorComparator<? super E>- the specified comparator. If it is null, the natural ordering will be used.
Throws
IllegalArgumentException- if the initialCapacity is less than 1.
PriorityQueue
public PriorityQueue(Collection<? extends E> c)Parameters
cCollection<? extends E>- the collection whose elements will be added to the priority queue to be constructed.
Throws
ClassCastException- if any of the elements in the collection are not comparable.
NullPointerException- if any of the elements in the collection are null.
PriorityQueue
public PriorityQueue(PriorityQueue<? extends E> c)Parameters
cPriorityQueue<? extends E>- the priority queue whose elements will be added to the priority queue to be constructed.
PriorityQueue
public PriorityQueue(SortedSet<? extends E> c)Parameters
cSortedSet<? extends E>- the sorted set whose elements will be added to the priority queue to be constructed.
Method details
iterator
public Iterator<E> iterator()Returns
size
public int size()Returns
clear
public void clear()Throws
UnsupportedOperationException- it the iterator does not support removing elements from
this
Collection
offer
public boolean offer(E o)Parameters
oE- the element to add to the priority queue.
Returns
Throws
ClassCastException- if the element cannot be compared with the elements in the priority queue using the ordering of the priority queue.
NullPointerException- if
oisnull.
poll
public E poll()Returns
peek
public E peek()Returns
comparator
public Comparator<? super E> comparator()Returns
remove
public boolean remove(Object o)Parameters
oObject- the object to be removed.
Returns
add
public boolean add(E o)Parameters
oE- the object to be added.
Returns
Throws
ClassCastException- if the element cannot be compared with the elements in the priority queue using the ordering of the priority queue.
NullPointerException- if
oisnull.
contains
public boolean contains(Object object)Parameters
objectObject- the object to search for.
Returns
true if object is an element of this Collection, false otherwise.Throws
ClassCastException- if the object to look for isn’t of the correct type.
NullPointerException- if the object to look for is
nulland thisCollectiondoesn’t supportnullelements.
toArray
public Object[] toArray()Returns
toArray
public <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. If the argument array is big enough, the elements from the queue will be stored in it(element immediately following the end of the queue is set to null, if any); otherwise, it will return a new array with the size of the argument array and size of the queue.
Type parameter T: the type of elements in the array
Parameters
arrayT[]- 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 used
Returns
Throws
ArrayStoreException- if the type of the argument array is not compatible with every element in the queue
NullPointerException- if the argument array is null