Class BoundedPriorityQueue<E>

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractQueue<E>
org.openjdk.jmh.util.BoundedPriorityQueue<E>
Type Parameters:
E - type of the element
All Implemented Interfaces:
Serializable, Iterable<E>, Collection<E>, Queue<E>

public class BoundedPriorityQueue<E> extends AbstractQueue<E> implements Serializable
Bounded variant of PriorityQueue. Note: elements are returned in reverse order. For instance, if "top N smallest elements" are required, use new BoundedPriorityQueue(N), and the elements would be returned in largest to smallest order.
See Also:
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      See Also:
    • maxSize

      private final int maxSize
    • comparator

      private final Comparator<? super E> comparator
    • queue

      private final Queue<E> queue
  • Constructor Details

    • BoundedPriorityQueue

      public BoundedPriorityQueue(int maxSize)
      Creates a bounded priority queue with the specified maximum size and default ordering. At most maxSize smallest elements would be kept in the queue.
      Parameters:
      maxSize - maximum size of the queue
    • BoundedPriorityQueue

      public BoundedPriorityQueue(int maxSize, Comparator<? super E> comparator)
      Creates a bounded priority queue with the specified maximum size. At most maxSize smallest elements would be kept in the queue.
      Parameters:
      maxSize - maximum size of the queue
      comparator - comparator that orders the elements
  • Method Details

    • reverse

      private static <E> Comparator<? super E> reverse(Comparator<? super E> comparator)
      Internal queue should be in fact in reverse order. By default, the queue aims for "top N smallest elements". So peek() should return the biggest element, so it can be removed when adding smaller element
      Type Parameters:
      E - type of the element
      Parameters:
      comparator - comparator that designates ordering of the entries or null for default ordering
      Returns:
      reverse comparator
    • add

      public boolean add(E e)
      Specified by:
      add in interface Collection<E>
      Specified by:
      add in interface Queue<E>
      Overrides:
      add in class AbstractQueue<E>
    • offer

      public boolean offer(E e)
      Specified by:
      offer in interface Queue<E>
    • poll

      public E poll()
      Specified by:
      poll in interface Queue<E>
    • peek

      public E peek()
      Specified by:
      peek in interface Queue<E>
    • iterator

      public Iterator<E> iterator()
      Specified by:
      iterator in interface Collection<E>
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in class AbstractCollection<E>
    • size

      public int size()
      Specified by:
      size in interface Collection<E>
      Specified by:
      size in class AbstractCollection<E>