E - the type of objects stored in this queuepublic class ShortPriorityQueue<E> extends AbstractSet<E> implements Queue<E>, SortedSet<E>
ShortPriorityQueue<E> is a length-bounded priority queue
optimized for short lengths. Rather than maintaining tree or heap
data structures, it keeps elements in an array and uses a simple
bubble sort.
This class is different than BoundedPriorityQueue in
that it does not attempt enforce the uniqueness required by
the Set interface.
For large queues, use BoundedPriorityQueue.
For large queues of Scored instances, use MinMaxHeap.
The head-, tail- and sub-set operations violate the sorted set interface's requirements that they be views onto the queue itself; instead, they are static snapshots.
O(n) where n is
the number of elements in the queue). The peek methods and first-
and last-element methods are all constant time, as is the
iterator's next method.| Constructor and Description |
|---|
ShortPriorityQueue(Comparator<? super E> comparator,
int maxSize)
Construct a short priority queue that compares elements using
the specified comparator with the specified maximum size.
|
| Modifier and Type | Method and Description |
|---|---|
void |
clear()
Removes all the elements from the priority queue.
|
Comparator<? super E> |
comparator()
Returns the comparator for this priority queue.
|
E |
element()
Returns the largest element in this queue according to
the comparator, throwing an exception if the queue is
empty.
|
E |
first()
Returns the largest element in the queue.
|
SortedSet<E> |
headSet(E toElement)
Return the set of elements in this queue strictly less than the
specified element according to the comparator for this queue.
|
boolean |
isEmpty()
Returns
true if the queue is empty. |
Iterator<E> |
iterator()
Returns an iterator over the elements in this queue.
|
E |
last()
Returns the smallest element in the queue.
|
int |
maxSize()
Returns the maximum size of this queue.
|
boolean |
offer(E elt)
Return
true if the specified element may be added to
the queue. |
E |
peek()
Returns the largest element in this queue according to
the comparator, or
null if the queue is empty. |
E |
peekLast()
Returns the smallest element in this queue according to
the comparator, or
null if the queue is empty. |
E |
poll()
Removes and returns the largest element in this queue, or returns
null if the queue is empty. |
E |
remove()
Returns and removes the head of this queue, throwing an
exception if it is empty.
|
boolean |
remove(Object obj)
Return
true and remove the largest element in the
queue equal to the specified object, or return false if
the specified object is not in the queue. |
int |
size()
Returns the current number of elements in the queue.
|
SortedSet<E> |
subSet(E fromElement,
E toElement)
Return the set of elements in this queue greater than or equal
to the specified lower bound and strictly less than the upper bound
element according to the comparator for this queue.
|
SortedSet<E> |
tailSet(E fromElement)
Return the set of elements in this queue grearer than or equal
to the specified element according to the comparator for this
queue.
|
String |
toString()
Returns a string-based representation of the elements in this queue.
|
equals, hashCode, removeAlladd, addAll, contains, containsAll, retainAll, toArray, toArraypublic ShortPriorityQueue(Comparator<? super E> comparator, int maxSize)
comparator - Element comparator for this queue.maxSize - Maximum number of elements in the queue.public int maxSize()
public int size()
size in interface Collection<E>size in interface Set<E>size in class AbstractCollection<E>public Iterator<E> iterator()
The iterator is not concurrent-modification safe, nor are there any heuristic checks to check for modifications and throw exceptions.
public void clear()
clear in interface Collection<E>clear in interface Set<E>clear in class AbstractCollection<E>public E peek()
null if the queue is empty.
This method differs from element() only in that it
throws an returns null if the queue is empty rather
than throwing an exception.
public E element()
This method differs from peek() only in
that it throws an exception if the queue is empty
rather than returning null.
element in interface Queue<E>NoSuchElementException - If the queue is empty.public E peekLast()
null if the queue is empty.public E first()
peek(),
this method throws an exception if the queue is empty.first in interface SortedSet<E>IllegalArgumentException - If the queue is empty.public E last()
peekLast(), this method throws an exception if the queue is
empty.last in interface SortedSet<E>IllegalArgumentException - If the queue is empty.public SortedSet<E> headSet(E toElement)
In violation of the SortedSet interface
specification, the result of this method is not a view
onto this queue, but rather a static snapshot of the queue.
headSet in interface SortedSet<E>toElement - Exclusive upper bound on returned elements.ClassCastException - If the upper bound is not compatible with
this queue's comparator.NullPointerException - If the upper bound is null.public SortedSet<E> tailSet(E fromElement)
In violation of the SortedSet interface
specification, the result of this method is not a view
onto this queue, but rather a static snapshot of the queue.
tailSet in interface SortedSet<E>fromElement - Inclusive lower bound on returned elements.ClassCastException - If the lower bound is not compatible with
this queue's comparator.NullPointerException - If the lower bound is null.public Comparator<? super E> comparator()
comparator in interface SortedSet<E>public SortedSet<E> subSet(E fromElement, E toElement)
In violation of the SortedSet interface
specification, the result of this method is not a view
onto this queue, but rather a static snapshot of the queue.
subSet in interface SortedSet<E>fromElement - Inclusive lower bound on returned elements.toElement - Exclusive upper bound on returned elements.ClassCastException - If the lower bound is not compatible with
this queue's comparator.NullPointerException - If the lower bound is null.IllegalArgumentException - If the lower bound is greater than the upper bound.public E poll()
null if the queue is empty.
This method differs from remove() only in that
it returns null if the queue is empty rather than
throwing an exception.
public E remove()
This method differs from poll() only in that
it throws an exception if the queue is empty rather than
returning null.
remove in interface Queue<E>NoSuchElementException - If the queue is emtpy.public boolean isEmpty()
true if the queue is empty.isEmpty in interface Collection<E>isEmpty in interface Set<E>isEmpty in class AbstractCollection<E>true if the queue is size zero.public boolean offer(E elt)
true if the specified element may be added to
the queue. An element may be added to the queue if the queue
is not full (its size is less than the maximum size), or if
the smallest element in the queue is strictly smaller than the
specified element according to this queue's comparator.public boolean remove(Object obj)
true and remove the largest element in the
queue equal to the specified object, or return false if
the specified object is not in the queue.remove in interface Collection<E>remove in interface Set<E>remove in class AbstractCollection<E>obj - Object to remove.true if the object was in the queue and was
removed.public String toString()
toString in class AbstractCollection<E>Copyright © 2016 Alias-i, Inc.. All rights reserved.