Class PriorityQueue<E>
- All Implemented Interfaces:
Iterable<E>, Collection<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.
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs 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 TypeMethodDescriptionbooleanAdds the specified object to the priority queue.voidclear()Removes all the elements of the priority queue.Comparator<? super E> Gets the comparator of the priority queue.booleanAnswers if there is an element in this queue equals to the object.iterator()Gets the iterator of the priority queue, which will not return elements in any specified ordering.booleanInserts the element to the priority queue.peek()Gets but does not remove the head of the queue.poll()Gets and removes the head of the queue.booleanRemoves the specified object from the priority queue.intsize()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.Methods inherited from class AbstractQueue
addAll, element, removeMethods inherited from class AbstractCollection
containsAll, isEmpty, removeAll, retainAll, toStringMethods inherited from class Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Collection
containsAll, equals, hashCode, isEmpty, removeAll, retainAll
-
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: @param 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: @param 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: @param 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: @param 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.
Returns
the iterator of the priority queue.
- Specified by:
iteratorin interfaceCollection<E>- Specified by:
iteratorin interfaceIterable<E>- Specified by:
iteratorin classAbstractCollection<E>
-
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.
Returns
the size of the priority queue.
- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- how many objects this
Collectioncontains, orInteger.MAX_VALUEif there are more thanInteger.MAX_VALUEelements in thisCollection.
-
clear
public void clear()Removes all the elements of the priority queue.- Specified by:
clearin interfaceCollection<E>- Overrides:
clearin classAbstractQueue<E>
-
offer
Inserts the element to the priority queue.
Parameters
o: the element to add to the priority queue.
Returns
always true
Throws
-
ClassCastException: @throws ClassCastException if the element cannot be compared with the elements in the priority queue using the ordering of the priority queue. -
NullPointerException: ifoisnull.
- Returns:
trueif the operation succeeds andfalseif it fails.
-
poll
Gets and removes the head of the queue.
Returns
the head of the queue or null if the queue is empty.
- Returns:
- the element at the head of the queue or
nullif there is no element in the queue.
-
peek
Gets but does not remove the head of the queue.
Returns
the head of the queue or null if the queue is empty.
- Returns:
- the element at the head of the queue or
nullif there is no element in the queue.
-
comparator
Gets the comparator of the priority queue.
Returns
- Returns:
- the comparator of the priority queue or null if the natural ordering is used.
-
remove
Removes the specified object from the priority queue.
Parameters
o: the object to be removed.
Returns
- Specified by:
removein interfaceCollection<E>- Overrides:
removein classAbstractCollection<E>- 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.
Parameters
o: the object to be added.
Returns
always true.
Throws
-
ClassCastException: @throws ClassCastException if the element cannot be compared with the elements in the priority queue using the ordering of the priority queue. -
NullPointerException: ifoisnull.
- Specified by:
addin interfaceCollection<E>- Overrides:
addin classAbstractQueue<E>- Returns:
trueif thisCollectionis modified,falseotherwise.Throws
-
UnsupportedOperationException: if adding to thisCollectionis not supported. -
ClassCastException: @throws ClassCastException if the class of the object is inappropriate for this collection. -
IllegalArgumentException: if the object cannot be added to thisCollection. -
NullPointerException: if null elements cannot be added to theCollection.
-
-
contains
Answers if there is an element in this queue equals to the object.
See also
- java.util.AbstractCollection#contains(java.lang.Object)
- Specified by:
containsin interfaceCollection<E>- Overrides:
containsin classAbstractCollection<E>- Returns:
trueif object is an element of thisCollection,falseotherwise.Throws
-
ClassCastException: @throws ClassCastException if the object to look for isn't of the correct type. -
NullPointerException: @throws NullPointerException if the object to look for isnulland thisCollectiondoesn't supportnullelements.
-
-
toArray
Returns all the elements in an array. The result is a copy of all the elements.
Returns
the Array of all the elements
See also
- java.util.AbstractCollection#toArray()
- Specified by:
toArrayin interfaceCollection<E>- Overrides:
toArrayin classAbstractCollection<E>
-
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.- Specified by:
toArrayin interfaceCollection<E>- Overrides:
toArrayin classAbstractCollection<E>- Parameters:
the-type of elements in the array
Parameters
array: @param array 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
the Array of all the elements
Throws
-
ArrayStoreException: @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
See also
- java.util.AbstractCollection#toArray(T[])
-