I have an array containing a big number of elements—more than 2,000,000. I need to obtain the highest (or lowest) ranking 300 elements. So whenever I reach the first highest (or lowest) 300 elements of the array, return them. Currently Arrays.sort is used for the whole array, then the highest (or lowest) ranking elements are returned.
e.g.: {1,2,3,4,5,6,7,8,9}
I want to obtain the highest 3 elements; I need: {9,8,7}
Any suggestions on this one?
EDIT
Best resource found so far, containing a research/comparison of different solutions:
http://www.michaelpollmeier.com/selecting-top-k-items-from-a-list-efficiently-in-java-groovy/
Source code for the article: