@Beta @GwtCompatible public final class MinMaxPriorityQueue<E> extends AbstractQueue<E>
Usage example:
MinMaxPriorityQueue<User> users = MinMaxPriorityQueue.orderedBy(userComparator)
.maximumSize(1000)
.create();
As a Queue
it functions exactly as a PriorityQueue
: its head element -- the
implicit target of the methods peek()
, poll()
and AbstractQueue.remove()
-- is
defined as the least element in the queue according to the queue's comparator. But unlike
a regular priority queue, the methods peekLast()
, pollLast()
and removeLast()
are also provided, to act on the greatest element in the queue instead.
A min-max priority queue can be configured with a maximum size. If so, each time the size of the queue exceeds that value, the queue automatically removes its greatest element according to its comparator (which might be the element that was just added). This is different from conventional bounded queues, which either block or reject new elements when full.
This implementation is based on the min-max heap developed by Atkinson, et al.
Unlike many other double-ended priority queues, it stores elements in a single array, as compact
as the traditional heap data structure used in PriorityQueue
.
This class is not thread-safe, and does not accept null elements.
Performance notes:
PriorityQueue
with manual eviction above the maximum
size. In many cases Ordering.leastOf(java.lang.Iterable<E>, int)
may work for your use case with significantly
improved (and asymptotically superior) performance.
peek()
, peekFirst()
, peekLast()
, AbstractQueue.element()
, and size
are constant-time.
offer(E)
, add(E)
, and all the forms of
poll()
and AbstractQueue.remove()
) run in O(log n) time
.
AbstractCollection.remove(Object)
and AbstractCollection.contains(java.lang.Object)
operations require linear (O(n)
)
time.
PriorityQueue
, but significantly slower.
Modifier and Type | Class and Description |
---|---|
static class |
MinMaxPriorityQueue.Builder<B>
The builder class used in creation of min-max priority queues.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E element)
Adds the given element to this queue.
|
boolean |
addAll(Collection<? extends E> newElements)
Adds all of the elements in the specified collection to this
queue.
|
void |
clear()
Removes all of the elements from this queue.
|
Comparator<? super E> |
comparator()
Returns the comparator used to order the elements in this queue.
|
static <E extends Comparable<E>> |
create()
Creates a new min-max priority queue with default settings: natural order, no maximum size, no
initial contents, and an initial expected size of 11.
|
static <E extends Comparable<E>> |
create(Iterable<? extends E> initialContents)
Creates a new min-max priority queue using natural order, no maximum size, and initially
containing the given elements.
|
static MinMaxPriorityQueue.Builder<Comparable> |
expectedSize(int expectedSize)
Creates and returns a new builder, configured to build
MinMaxPriorityQueue instances
sized appropriately to hold expectedSize elements. |
Iterator<E> |
iterator()
Returns an iterator over the elements contained in this collection, in no particular
order.
|
static MinMaxPriorityQueue.Builder<Comparable> |
maximumSize(int maximumSize)
Creates and returns a new builder, configured to build
MinMaxPriorityQueue instances
that are limited to maximumSize elements. |
boolean |
offer(E element)
Adds the given element to this queue.
|
static <B> MinMaxPriorityQueue.Builder<B> |
orderedBy(Comparator<B> comparator)
Creates and returns a new builder, configured to build
MinMaxPriorityQueue instances
that use comparator to determine the least and greatest elements. |
E |
peek()
Retrieves, but does not remove, the head of this queue,
or returns
null if this queue is empty. |
E |
peekFirst()
Retrieves, but does not remove, the least element of this queue, or returns
null if the
queue is empty. |
E |
peekLast()
Retrieves, but does not remove, the greatest element of this queue, or returns
null if
the queue is empty. |
E |
poll()
Retrieves and removes the head of this queue,
or returns
null if this queue is empty. |
E |
pollFirst()
Removes and returns the least element of this queue, or returns
null if the queue is
empty. |
E |
pollLast()
Removes and returns the greatest element of this queue, or returns
null if the queue is
empty. |
E |
removeFirst()
Removes and returns the least element of this queue.
|
E |
removeLast()
Removes and returns the greatest element of this queue.
|
int |
size()
Returns the number of elements in this collection.
|
Object[] |
toArray()
Returns an array containing all of the elements in this collection.
|
element, remove
contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toString
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
contains, containsAll, equals, hashCode, isEmpty, parallelStream, remove, removeAll, removeIf, retainAll, spliterator, stream, toArray
public static <E extends Comparable<E>> MinMaxPriorityQueue<E> create()
public static <E extends Comparable<E>> MinMaxPriorityQueue<E> create(Iterable<? extends E> initialContents)
public static <B> MinMaxPriorityQueue.Builder<B> orderedBy(Comparator<B> comparator)
MinMaxPriorityQueue
instances
that use comparator
to determine the least and greatest elements.public static MinMaxPriorityQueue.Builder<Comparable> expectedSize(int expectedSize)
MinMaxPriorityQueue
instances
sized appropriately to hold expectedSize
elements.public static MinMaxPriorityQueue.Builder<Comparable> maximumSize(int maximumSize)
MinMaxPriorityQueue
instances
that are limited to maximumSize
elements. Each time a queue grows beyond this bound, it
immediately removes its greatest element (according to its comparator), which might be the
element that was just added.public int size()
java.util.Collection
size
in interface Collection<E>
size
in class AbstractCollection<E>
@CanIgnoreReturnValue public boolean add(E element)
element
the queue will automatically evict its greatest element (according to its comparator),
which may be element
itself.add
in interface Collection<E>
add
in interface Queue<E>
add
in class AbstractQueue<E>
element
- the element to addtrue
always@CanIgnoreReturnValue public boolean addAll(Collection<? extends E> newElements)
java.util.AbstractQueue
This implementation iterates over the specified collection, and adds each element returned by the iterator to this queue, in turn. A runtime exception encountered while trying to add an element (including, in particular, a null element) may result in only some of the elements having been successfully added when the associated exception is thrown.
addAll
in interface Collection<E>
addAll
in class AbstractQueue<E>
newElements
- collection containing elements to be added to this queueAbstractQueue.add(Object)
@CanIgnoreReturnValue public boolean offer(E element)
element
the queue will automatically evict its greatest element (according to its comparator),
which may be element
itself.element
- the element to addtrue
if the element was added to this queue, else
false
@CanIgnoreReturnValue public E poll()
java.util.Queue
null
if this queue is empty.null
if this queue is emptypublic E peek()
java.util.Queue
null
if this queue is empty.null
if this queue is empty@CanIgnoreReturnValue public E pollFirst()
null
if the queue is
empty.@CanIgnoreReturnValue public E removeFirst()
NoSuchElementException
- if the queue is emptypublic E peekFirst()
null
if the
queue is empty.@CanIgnoreReturnValue public E pollLast()
null
if the queue is
empty.@CanIgnoreReturnValue public E removeLast()
NoSuchElementException
- if the queue is emptypublic E peekLast()
null
if
the queue is empty.public Iterator<E> iterator()
The iterator is fail-fast: If the MinMaxPriorityQueue is modified at any time after
the iterator is created, in any way except through the iterator's own remove method, the
iterator will generally throw a ConcurrentModificationException
. Thus, in the face of
concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary,
non-deterministic behavior at an undetermined time in the future.
Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally
speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent
modification. Fail-fast iterators throw ConcurrentModificationException
on a
best-effort basis. Therefore, it would be wrong to write a program that depended on this
exception for its correctness: the fail-fast behavior of iterators should be used only to
detect bugs.
iterator
in interface Iterable<E>
iterator
in interface Collection<E>
iterator
in class AbstractCollection<E>
public void clear()
java.util.AbstractQueue
This implementation repeatedly invokes poll
until it
returns null.
clear
in interface Collection<E>
clear
in class AbstractQueue<E>
public Object[] toArray()
java.util.AbstractCollection
The returned array will be "safe" in that no references to it are maintained by this collection. (In other words, this method must allocate a new array even if this collection is backed by an array). The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based APIs.
This implementation returns an array containing all the elements
returned by this collection's iterator, in the same order, stored in
consecutive elements of the array, starting with index 0
.
The length of the returned array is equal to the number of elements
returned by the iterator, even if the size of this collection changes
during iteration, as might happen if the collection permits
concurrent modification during iteration. The size
method is
called only as an optimization hint; the correct result is returned
even if the iterator returns a different number of elements.
This method is equivalent to:
List<E> list = new ArrayList<E>(size());
for (E e : this)
list.add(e);
return list.toArray();
toArray
in interface Collection<E>
toArray
in class AbstractCollection<E>
public Comparator<? super E> comparator()
PriorityQueue.comparator
, but returns Ordering.natural()
instead of null
to indicate natural ordering.Copyright © 2010–2018. All rights reserved.