Package java.util
Class PriorityQueue<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractQueue<E>
java.util.PriorityQueue<E>
- All Implemented Interfaces:
Serializable,Iterable<E>,Collection<E>,Queue<E>
public class PriorityQueue<E> extends AbstractQueue<E> implements Serializable
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 the first retrieved with
poll() and the greatest element is the last.
A PriorityQueue is not synchronized. If multiple threads will have to access
it concurrently, use the PriorityBlockingQueue.
- See Also:
- Serialized Form
-
Constructor Summary
Constructors Constructor Description PriorityQueue()Constructs a priority queue with an initial capacity of 11 and natural ordering.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(Collection<? extends E> c)Constructs a priority queue that contains the elements of a collection.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. -
Method Summary
Modifier and Type Method Description booleanadd(E o)Adds the specified object to the priority queue.voidclear()Removes all the elements of the priority queue.Comparator<? super E>comparator()Gets the comparator of the priority queue.Iterator<E>iterator()Gets the iterator of the priority queue, which will not return elements in any specified ordering.booleanoffer(E o)Inserts the element to the priority queue.Epeek()Gets but does not remove the head of the queue.Epoll()Gets and removes the head of the queue.booleanremove(Object o)Removes the specified object from the priority queue.intsize()Gets the size of the priority queue.Methods inherited from class java.util.AbstractQueue
addAll, element, removeMethods inherited from class java.util.AbstractCollection
contains, containsAll, isEmpty, removeAll, retainAll, toArray, toArray, toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
contains, containsAll, equals, hashCode, isEmpty, removeAll, retainAll, toArray, toArray
-
Constructor Details
-
PriorityQueue
public PriorityQueue()Constructs a priority queue with an initial capacity of 11 and natural ordering. -
PriorityQueue
public PriorityQueue(int initialCapacity)Constructs a priority queue with the specified capacity and natural ordering.- Parameters:
initialCapacity- the specified capacity.- Throws:
IllegalArgumentException- if the initialCapacity is less than 1.
-
PriorityQueue
Constructs a priority queue with the specified capacity and comparator.- Parameters:
initialCapacity- the specified capacity.comparator- the specified comparator. If it is null, the natural ordering will be used.- Throws:
IllegalArgumentException- if the initialCapacity is less than 1.
-
PriorityQueue
Constructs a priority queue that contains the elements of a collection. The constructed priority queue has the initial capacity of 110% of the size of the collection. The queue uses natural ordering to order its elements.- Parameters:
c- 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
Constructs a priority queue that contains the elements of another priority queue. The constructed priority queue has the initial capacity of 110% of the specified one. Both priority queues have the same comparator.- Parameters:
c- the priority queue whose elements will be added to the priority queue to be constructed.
-
PriorityQueue
Constructs a priority queue that contains the elements of a sorted set. The constructed priority queue has the initial capacity of 110% of the size of the sorted set. The priority queue will have the same comparator as the sorted set.- Parameters:
c- the sorted set whose elements will be added to the priority queue to be constructed.
-
-
Method Details
-
iterator
Gets the iterator of the priority queue, which will not return elements in any specified ordering.- Specified by:
iteratorin interfaceCollection<E>- Specified by:
iteratorin interfaceIterable<E>- Specified by:
iteratorin classAbstractCollection<E>- Returns:
- the iterator of the priority queue.
-
size
public int size()Gets the size of the priority queue. If the size of the queue is greater than the Integer.MAX, then it returns Integer.MAX.- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- the size of the priority queue.
-
clear
public void clear()Removes all the elements of the priority queue.- Specified by:
clearin interfaceCollection<E>- Overrides:
clearin classAbstractQueue<E>- See Also:
AbstractCollection.iterator(),AbstractCollection.isEmpty(),AbstractCollection.size()
-
offer
Inserts the element to the priority queue.- Specified by:
offerin interfaceQueue<E>- Parameters:
o- the element to add to the priority queue.- Returns:
- always true
- Throws:
ClassCastException- if the element cannot be compared with the elements in the priority queue using the ordering of the priority queue.NullPointerException- ifoisnull.
-
poll
Gets and removes the head of the queue. -
peek
Gets but does not remove the head of the queue. -
comparator
Gets the comparator of the priority queue.- Returns:
- the comparator of the priority queue or null if the natural ordering is used.
-
remove
Removes the specified object from the priority queue.- Specified by:
removein interfaceCollection<E>- Overrides:
removein classAbstractCollection<E>- Parameters:
o- the object to be removed.- Returns:
- true if the object was in the priority queue, false if the object was not in the priority queue.
-
add
Adds the specified object to the priority queue.- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceQueue<E>- Overrides:
addin classAbstractQueue<E>- Parameters:
o- the object to be added.- Returns:
- always true.
- Throws:
ClassCastException- if the element cannot be compared with the elements in the priority queue using the ordering of the priority queue.NullPointerException- ifoisnull.
-