title: Get top 10 and last 10 from a million records
body: <p>I have a report which shows 2-4 million records. I get the records from oracle to java and push it to an excel report. All this is already done!</p>
  
  <p>Now, I also need to add a new tab with top 10 and last 10 records. What would be the best way to do it?</p>
  
  <p>Should i use PriorityQueue implementation in java or use a binary tree to keep a track of top 10 and last 10. I don't need to store the billion records in the data structure. I just need to save 10 at a time. 
  ex: </p>
  
  <pre><code>PriorityQueue&lt;DataObject&gt; queueTop10 = new PriorityQueue&lt;DataObject&gt;(10, topComparator);
  PriorityQueue&lt;DataObject&gt; queueLast10 = new PriorityQueue&lt;DataObject&gt;(10, leastComparator);
      while (data is coming from database)
      {
      // push to excel stuff here
      queueTop10 .add(dataObject);   OR binarytreeTop.insert(dataObject)
      queueLast10.add(dataObject);   OR binarytreeLeast.insert(dataObject)
      }
  </code></pre>
  
  <p>Please let me know if i can use some other data structure as well. </p>
  
  <p>Thanks</p>