从地图中检索所有条目< Integer,String>键在一定范围内
我需要一个HashMap<整数,字符串>,它可以提供快速操作,用于检索其键位于某个整数范围内的所有条目的列表,此外还可以根据键从地图获取值。
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.
Map
实现适合这些需求?
c $ c> TreeMap ,它实现 NavigableMap
提供 subMap 方法只返回地图视图范围。要获取这些值,当然在结果中调用 values()
。
Use a TreeMap
, which implements NavigableMap
supplying a subMap method returning a view of the map with only keys in your range. To get the values, of course you call values()
on the result.
如果你有一个映射
其键实现 Comparable
,您可以构造一个 TreeMap
它通过调用新的TreeMap(existingMap)
,但它可能会更有效地创建一个 TreeMap
开始。
If you have an existing Map
whose keys implement Comparable
, you can construct a TreeMap
from it by calling new TreeMap(existingMap)
, but it will likely be more efficient to create it as a TreeMap
from the start.