Class BoundedPriorityQueue<E>

  • Type Parameters:
    E - type of the element
    All Implemented Interfaces:
    java.io.Serializable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.Queue<E>

    public class BoundedPriorityQueue<E>
    extends java.util.AbstractQueue<E>
    implements java.io.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:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.util.Comparator<? super E> comparator  
      private int maxSize  
      private java.util.Queue<E> queue  
      private static long serialVersionUID  
    • Constructor Summary

      Constructors 
      Constructor Description
      BoundedPriorityQueue​(int maxSize)
      Creates a bounded priority queue with the specified maximum size and default ordering.
      BoundedPriorityQueue​(int maxSize, java.util.Comparator<? super E> comparator)
      Creates a bounded priority queue with the specified maximum size.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(E e)  
      java.util.Iterator<E> iterator()  
      boolean offer​(E e)  
      E peek()  
      E poll()  
      private static <E> java.util.Comparator<? super E> reverse​(java.util.Comparator<? super E> comparator)
      Internal queue should be in fact in reverse order.
      int size()  
      • Methods inherited from class java.util.AbstractQueue

        addAll, clear, element, remove
      • Methods inherited from class java.util.AbstractCollection

        contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        contains, containsAll, equals, hashCode, isEmpty, parallelStream, remove, removeAll, removeIf, retainAll, spliterator, stream, toArray, toArray, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
    • Field Detail

      • maxSize

        private final int maxSize
      • comparator

        private final java.util.Comparator<? super E> comparator
      • queue

        private final java.util.Queue<E> queue
    • Constructor Detail

      • 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,
                                    java.util.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 Detail

      • reverse

        private static <E> java.util.Comparator<? super E> reverse​(java.util.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 java.util.Collection<E>
        Specified by:
        add in interface java.util.Queue<E>
        Overrides:
        add in class java.util.AbstractQueue<E>
      • offer

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

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

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

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

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