Class MinMaxPriorityQueue<E>

  • All Implemented Interfaces:
    Iterable<E>, Collection<E>, Queue<E>

    @Beta
    public final class MinMaxPriorityQueue<E>
    extends AbstractQueue<E>
    A double-ended priority queue, which provides constant-time access to both its least element and its greatest element, as determined by the queue's specified comparator. If no comparator is given at construction time, the natural order of elements is used.

    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:

    Since:
    8.0
    • Method Detail

      • create

        public static <E extends Comparable<E>> MinMaxPriorityQueue<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.
      • create

        public static <E extends Comparable<E>> MinMaxPriorityQueue<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.
      • orderedBy

        public 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.
      • expectedSize

        public static MinMaxPriorityQueue.Builder<Comparable> expectedSize​(int expectedSize)
        Creates and returns a new builder, configured to build MinMaxPriorityQueue instances sized appropriately to hold expectedSize elements.
      • maximumSize

        public static MinMaxPriorityQueue.Builder<Comparable> maximumSize​(int maximumSize)
        Creates and returns a new builder, configured to build 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.
      • add

        public boolean add​(E element)
        Adds the given element to this queue. If this queue has a maximum size, after adding element the queue will automatically evict its greatest element (according to its comparator), which may be element itself.
        Specified by:
        add in interface Collection<E>
        Specified by:
        add in interface Queue<E>
        Overrides:
        add in class AbstractQueue<E>
        Returns:
        true always
      • offer

        public boolean offer​(E element)
        Adds the given element to this queue. If this queue has a maximum size, after adding element the queue will automatically evict its greatest element (according to its comparator), which may be element itself.
      • poll

        public E poll()
      • peek

        public E peek()
      • pollFirst

        public E pollFirst()
        Removes and returns the least element of this queue, or returns null if the queue is empty.
      • removeFirst

        public E removeFirst()
        Removes and returns the least element of this queue.
        Throws:
        NoSuchElementException - if the queue is empty
      • peekFirst

        public E peekFirst()
        Retrieves, but does not remove, the least element of this queue, or returns null if the queue is empty.
      • pollLast

        public E pollLast()
        Removes and returns the greatest element of this queue, or returns null if the queue is empty.
      • removeLast

        public E removeLast()
        Removes and returns the greatest element of this queue.
        Throws:
        NoSuchElementException - if the queue is empty
      • peekLast

        public E peekLast()
        Retrieves, but does not remove, the greatest element of this queue, or returns null if the queue is empty.
      • iterator

        public Iterator<E> iterator()
        Returns an iterator over the elements contained in this collection, in no particular order.

        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.

        Specified by:
        iterator in interface Collection<E>
        Specified by:
        iterator in interface Iterable<E>
        Specified by:
        iterator in class AbstractCollection<E>
        Returns:
        an iterator over the elements contained in this collection