title: Java Collection - Top and Bottom n elements
body: <p>I have a rather unique requirement where in my collection should hold only the Top and Bottom n elements. The elements are comparable and the Collection itself is bounded which means the evaluation is done at the time of adding an entry to the Collection. </p>
  
  <p>For example when the following set of values are inserted in to a "Top and Bottom 10" Collection</p>
  
  <p>5, 15, 10, 1, 12, 8, 11, 2, 16, 14, 9, 3, 20, 7 </p>
  
  <p>the Collection should hold only the following</p>
  
  <p>20, 16, 15, 14, 12, 7, 5, 3, 2, 1</p>
  
  <p>I was thinking of maintaining 2 SortedSet of n/2 elements and then merging them in the end however the approach isnt clean and requires the merge step before consuming the results.</p>
  
  <p>Just hoping someone would have a better answer to this problem.</p>