T
- public class BoundedPriorityQueue<T> extends InvertedPriorityQueue<T>
InvertedPriorityQueue
.
Insertions to the queue are worst-case O(log(N)) and O(1) if the insertion is
rejected. peek()
and poll()
are very inefficient (O(N)) as
they have to search for the head of the queue. peekTail()
and
pollTail()
have complexity O(1) and O(log(N)) respectively.
This implementation is ideally suited for storing the top-N items of a process; that is, where items are constantly added, but not removed very often.
The class contains a number of utility methods to get a sorted copy of the queue.
The Iterator provided in method PriorityQueue.iterator()
is not
guaranteed to traverse the elements of the priority queue in any particular
order.
Constructor and Description |
---|
BoundedPriorityQueue(int maxSize)
Creates a
BoundedPriorityQueue with the specified initial
capacity that orders its elements according to their inverse
natural ordering. |
BoundedPriorityQueue(int maxSize,
Comparator<? super T> comparator)
Creates a
BoundedPriorityQueue with the specified initial
capacity that orders its elements according to the inverse of the
specified comparator. |
Modifier and Type | Method and Description |
---|---|
boolean |
isFull()
Is the
BoundedPriorityQueue full? |
boolean |
offer(T e) |
T |
offerItem(T item)
Inserts the specified element into this priority queue if possible.
|
T |
peek()
Peak at the head of the queue.
|
T |
peekTail()
Retrieves, but does not remove, the tail of this queue, or returns
null if this queue is empty.
|
T |
poll()
Poll the head of the queue.
|
T |
pollTail()
Retrieves and remove the tail of this queue, or returns null if
this queue is empty.
|
Object[] |
toOrderedArray()
Returns an array containing all of the elements in this queue.
|
T[] |
toOrderedArray(T[] a)
Returns a sorted array containing all of the elements in this queue; the
runtime type of the returned array is that of the specified array.
|
Object[] |
toOrderedArrayDestructive()
Returns an array containing all of the elements in this queue.
|
T[] |
toOrderedArrayDestructive(T[] a)
Returns a sorted array containing all of the elements in this queue; the
runtime type of the returned array is that of the specified array.
|
List<T> |
toOrderedList()
Create a new list with the contents of the queue and sort them into their
natural order, or the order specified by the
Comparator used in
constructing the queue. |
List<T> |
toOrderedListDestructive()
Create a new list with the contents of the queue with the elements
inserted in their natural order, or the order specified by the
Comparator used in constructing the queue. |
originalComparator
add, clear, comparator, contains, iterator, remove, size, spliterator, toArray, toArray
addAll, element, remove
containsAll, isEmpty, removeAll, retainAll, toString
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
containsAll, equals, hashCode, isEmpty, parallelStream, removeAll, removeIf, retainAll, stream
public BoundedPriorityQueue(int maxSize, Comparator<? super T> comparator)
BoundedPriorityQueue
with the specified initial
capacity that orders its elements according to the inverse of the
specified comparator.maxSize
- the maximum number of elements in this priority queuecomparator
- the comparator that will be used to order this priority queue.
If null
, the natural ordering
of the elements will be used. Internally, the comparator is
inverted to reverse its meaning.IllegalArgumentException
- if initialCapacity
is less than 1public BoundedPriorityQueue(int maxSize)
BoundedPriorityQueue
with the specified initial
capacity that orders its elements according to their inverse
natural ordering.maxSize
- the maximum number of elements in this priority queueIllegalArgumentException
- if initialCapacity
is less than 1public T peek()
PriorityQueue
, this is the
Peeking at the head is an expensive (O(n)) operation as the
BoundedPriorityQueue
only maintains the tail for fast insertions.
peek
in interface Queue<T>
peek
in class PriorityQueue<T>
PriorityQueue.peek()
public T poll()
PriorityQueue
, this is the
Polling the head is an expensive (O(n)) operation as the
BoundedPriorityQueue
only maintains the tail for fast insertions.
poll
in interface Queue<T>
poll
in class PriorityQueue<T>
PriorityQueue.poll()
public List<T> toOrderedList()
Comparator
used in
constructing the queue. The list constructed in O(N) time, and the
sorting takes O(log(N)) time.public Object[] toOrderedArray()
Comparator
used in constructing the queue. The array is
constructed in O(N) time, and the sorting takes O(log(N)) time.
The returned array will be "safe" in that no references to it are maintained by this queue. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based APIs.
public T[] toOrderedArray(T[] a)
The elements are in sorted into their natural order, or the order
specified by the Comparator
used in constructing the queue. The
array is constructed in O(N) time, and the sorting takes O(log(N)) time.
If the queue fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this queue.
If the queue fits in the specified array with room to spare (i.e., the
array has more elements than the queue), the element in the array
immediately following the end of the collection is set to null
.
Like the PriorityQueue.toArray()
method, this method acts as bridge between
array-based and collection-based APIs. Further, this method allows
precise control over the runtime type of the output array, and may, under
certain circumstances, be used to save allocation costs.
Suppose x is a queue known to contain only strings. The following code can be used to dump the queue into a newly allocated array of String:
String[] y = x.toArray(new String[0]);
Note that toArray(new Object[0]) is identical in function to toArray().
a
- the array into which the elements of the queue are to be
stored, if it is big enough; otherwise, a new array of the
same runtime type is allocated for this purpose.ArrayStoreException
- if the runtime type of the specified array is not a supertype
of the runtime type of every element in this queueNullPointerException
- if the specified array is nullpublic List<T> toOrderedListDestructive()
Comparator
used in constructing the queue.
This method destroys the queue; after the operation completes, the queue will be empty. The operation completes in O(Nlog(N)) time.
public Object[] toOrderedArrayDestructive()
Comparator
used in constructing the queue.
This method destroys the queue; after the operation completes, the queue will be empty. The operation completes in O(Nlog(N)) time.
The returned array will be "safe" in that no references to it are maintained by this queue. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based APIs.
public T[] toOrderedArrayDestructive(T[] a)
The elements are in sorted into their natural order, or the order
specified by the Comparator
used in constructing the queue.
This method destroys the queue; after the operation completes, the queue will be empty. The operation completes in O(Nlog(N)) time.
If the queue fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this queue.
If the queue fits in the specified array with room to spare (i.e., the
array has more elements than the queue), the element in the array
immediately following the end of the collection is set to null
.
Like the PriorityQueue.toArray()
method, this method acts as bridge between
array-based and collection-based APIs. Further, this method allows
precise control over the runtime type of the output array, and may, under
certain circumstances, be used to save allocation costs.
Suppose x is a queue known to contain only strings. The following code can be used to dump the queue into a newly allocated array of String:
String[] y = x.toArray(new String[0]);
Note that toArray(new Object[0]) is identical in function to toArray().
a
- the array into which the elements of the queue are to be
stored, if it is big enough; otherwise, a new array of the
same runtime type is allocated for this purpose.ArrayStoreException
- if the runtime type of the specified array is not a supertype
of the runtime type of every element in this queueNullPointerException
- if the specified array is nullpublic T peekTail()
This operation is performed in O(1) time.
public T pollTail()
This operation is performed in O(1) time.
public T offerItem(T item)
PriorityQueue.add(Object)
or
offer(Object)
, but returns a different value depending on the
queue size and whether the item was successfully added.
Specifically, if the item was not added, then it is returned. If the item
was added to a queue that has not reached its capacity, then
null
is returned. If the item was added to a queue at
capacity, then the item that was removed to make space is returned.
item
- The item to attempt to add.ClassCastException
- if the specified element cannot be compared with elements
currently in this priority queue according to the priority
queue's orderingNullPointerException
- if the specified element is nullpublic boolean isFull()
BoundedPriorityQueue
full?