posted 14 years ago
With a normal HashMap I don't think you've got any better options than iterating through the Map, checking the key, and removing the ones you don't want. I'd suggest using the entrySet to do this. E.g.
It would be different if you had a SortedMap or NavigableMap (e.g. TreeMap). These are ordered according to their keys, and there are methods available to give you a view onto a sub-map, which you can then clear. E.g.
That won't be more efficient, but it's more concise. If you're not able to switch the type of map in the first place it's not much help to you, though, I'm afraid.