title: Java: Collection of N highest elements
body: <p>I need to find N largest elements in a big collection of data.</p>
  
  <p><strong>I have:</strong></p>
  
  <ul>
  <li>A collection of hundreds of millions items in external database (Cassandra)</li>
  <li><p>A job that iterates through these items and finds item with largest value</p>
  
  <pre><code>Item largest = null;
  
  // Page through big data
  List&lt;Item&gt; items = getNextPage(pageSize);
  while (items.size() &gt; 0) {
  
    // Update largest item based on values from current page
    for (Item current : items) {
      if (largest == null || largest.getValue() &lt; current.getValue()) {
        largest = current;
      }
    }
  
    // Move to next page
    items = getNextPage(pageSize);
  }
  </code></pre></li>
  </ul>
  
  <p><strong>I need:</strong></p>
  
  <ul>
  <li>Extend this job to hold N (lets say 100) elements with highest value</li>
  </ul>
  
  <p><strong>My approach:</strong></p>
  
  <ul>
  <li><p>I was thinking about something like priority queue with fixed size</p>
  
  <pre><code>class PQsort implements Comparator&lt;Item&gt; {
  
    public int compare(Item one, Item two) {
      return two.getValue() - one.getValue();
    }
  }
  
  PriorityQueue&lt;Item&gt; pq = new PriorityQueue&lt;Item&gt;(101, new PQsort());
  
  ...while...for...
  
  pq.offer(current);
  if (pq.size() == 101) {
    // Remove the tail somehow 
  }
  ...
  </code></pre></li>
  <li><p>Removing the tail: <a href="https://stackoverflow.com/questions/15117246/removing-tail-element-of-priority-queue">Removing tail element of priority queue</a></p></li>
  </ul>
  
  <p>What is the optimal solution for this task?</p>