title: Is there a PriorityQueue implementation with fixed capacity and custom comparator?
body: <p>Related questions: </p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/1846225/java-priorityqueue-with-fixed-size">Java PriorityQueue with fixed size</a></li>
  <li><a href="https://stackoverflow.com/questions/683041/java-how-do-i-use-a-priorityqueue">How do I use a PriorityQueue?</a></li>
  <li><a href="https://stackoverflow.com/questions/2784218/get-indexes-of-n-smallest-elements-in-an-array">get indexes of n smallest elements in an array</a></li>
  <li><a href="https://stackoverflow.com/questions/789250/scala-is-there-a-way-to-use-priorityqueue-like-i-would-in-java">Scala: Is there a way to use PriorityQueue like I would in Java?</a></li>
  </ul>
  
  <p>I have a <strong>very large data set</strong> (more than 5 millions items) and I need to get <strong>N largest</strong> items from it. The most natural way to do it is to use heap/priority queue <strong>storing only top N items</strong>. There are several good implementations of priority queue for JVM (Scala/Java), namely: </p>
  
  <ul>
  <li><a href="http://www.scala-lang.org/api/current/scala/collection/mutable/PriorityQueue.html" rel="noreferrer">scala.collection.mutable.PriorityQueue</a></li>
  <li><a href="http://download.oracle.com/javase/6/docs/api/java/util/PriorityQueue.html" rel="noreferrer">java.util.PriorityQueue</a></li>
  <li><a href="https://lucene.apache.org/core/6_5_1/core/org/apache/lucene/util/PriorityQueue.html" rel="noreferrer">lucene.util.PriorityQueue</a></li>
  </ul>
  
  <p>First 2 are nice, but they store all the items, which in my case gives critical memory overhead. Third (Lucene implementation) doesn't have such a drawback, but as I can see from documentation it also doesn't support custom comparator, which makes it useless for me. </p>
  
  <p>So, my question is: Is there a <strong><code>PriorityQueue</code> implementation</strong> with <strong>fixed capacity</strong> and <strong>custom comparator</strong>?</p>
  
  <p><strong>UPD.</strong> Finally I've created my own implementation, based on Peter's answer:</p>
  
  <pre><code>public class FixedSizePriorityQueue&lt;E&gt; extends TreeSet&lt;E&gt; {
  
      private int elementsLeft;
  
      public FixedSizePriorityQueue(int maxSize) {
          super(new NaturalComparator());
          this.elementsLeft = maxSize;
      }
  
      public FixedSizePriorityQueue(int maxSize, Comparator&lt;E&gt; comparator) {
          super(comparator);
          this.elementsLeft = maxSize;
      }
  
  
      /**
       * @return true if element was added, false otherwise
       * */
      @Override
      public boolean add(E e) {
          if (elementsLeft == 0 &amp;&amp; size() == 0) {
              // max size was initiated to zero =&gt; just return false
              return false;
          } else if (elementsLeft &gt; 0) {
              // queue isn't full =&gt; add element and decrement elementsLeft
              boolean added = super.add(e);
              if (added) {
                  elementsLeft--;
              }
              return added;
          } else {
              // there is already 1 or more elements =&gt; compare to the least
              int compared = super.comparator().compare(e, this.first());
              if (compared == 1) {
                  // new element is larger than the least in queue =&gt; pull the least and add new one to queue
                  pollFirst();
                  super.add(e);
                  return true;
              } else {
                  // new element is less than the least in queue =&gt; return false
                  return false;
              }
          }
      }
  }
  </code></pre>
  
  <p>(where <code>NaturalComparator</code> is taken from <a href="https://stackoverflow.com/questions/3241063/does-a-natural-comparator-exist-in-the-standard-api">this</a> question)</p>