I have a map I’ve populated (say it’s a HashMap). I want it cut down to size… I don’t care which elements I remove, I just want k of the elements removed.
What’s the most efficient way to do this (other than iterating)?
Edit: k is not known in advance. Suggestions to based on other kinds of maps are relevant.
A generic hashmap has only a limited number of interface functions. If K is small, I don’t see any obvious better way than iterating, and breaking out of the iteration once K keys are removed. Of course, if K is large it might be better to do something else, such as preserve size-k elements and clear. If you need particular characteristics, your own hashmap could have whatever characteristics you need.