Is it possible to extract the top n (I use top because I believe a TreeMap is sorted) key elements from a TreeMap, without iterating using an Iterator object.
I can do the iteration but it’s tedious to have to check for nulls etc.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Why would you be checking for nulls?
If you use the Google Collections Library you can use Iterables.limit.
EDIT: For some reason the GCL page doesn’t include
limit, but it’s in Guava (which has effectively replaced the GCL) – it’s stillIterables.limit.