I need to have a HashMap< Integer, String> which can serve fast operations for retrieving a list of all entries whose keys are in a certain integer range besides, getting values from map based on keys.
What Map implementation is suitable for these needs ?
Use a
TreeMap, which implementsNavigableMapsupplying a subMap method returning a view of the map with only keys in your range. To get the values, of course you callvalues()on the result.If you have an existing
Mapwhose keys implementComparable, you can construct aTreeMapfrom it by callingnew TreeMap(existingMap), but it will likely be more efficient to create it as aTreeMapfrom the start.