Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中从映射中获取值的范围_Java_Algorithm_Map - Fatal编程技术网

在Java中从映射中获取值的范围

在Java中从映射中获取值的范围,java,algorithm,map,Java,Algorithm,Map,是否可以获得与Java映射中的一系列键匹配的值。假设我有 Map<Key,Value> //size 10,000 Key - 9.0, 9.1, 9.5, 4.2, 4.3, 6.1, 6.6 Value - 10 , 20 , 30 , 40 , 20 , 60 , 10 ArrayList alMatch = {1.0,4.0,6.0} Map//大小10000 键-9.0、9.1、9.5、4.2、4.3、6.1、6.6 值-10,20,30,40,20,60,10

是否可以获得与Java映射中的一系列键匹配的值。假设我有

Map<Key,Value> //size 10,000
Key   - 9.0, 9.1, 9.5, 4.2, 4.3, 6.1, 6.6
Value - 10 , 20 , 30 , 40 , 20 , 60 , 10  

ArrayList alMatch = {1.0,4.0,6.0}
Map//大小10000
键-9.0、9.1、9.5、4.2、4.3、6.1、6.6
值-10,20,30,40,20,60,10
ArrayList alMatch={1.0,4.0,6.0}
在这种情况下,对于值4.0,我希望得到40(键4.2)和20(键4.3)。所以我想得到映射到映射中键
5.0>=key>=4.0
的所有值。是否可以通过地图或类似的数据结构来实现这一点


这张地图很大。或者有没有其他更好的方法以最小的复杂性实现同样的功能。

您可以使用NavigableMap(示例TreeMap)的实现。您可能特别感兴趣的是这种方法:

/**
 * Returns a view of the portion of this map whose keys range from
 * {@code fromKey} to {@code toKey}.  If {@code fromKey} and
 * {@code toKey} are equal, the returned map is empty unless
 * {@code fromExclusive} and {@code toExclusive} are both true.  The
 * returned map is backed by this map, so changes in the returned map are
 * reflected in this map, and vice-versa.  The returned map supports all
 * optional map operations that this map supports.
 *
 * <p>The returned map will throw an {@code IllegalArgumentException}
 * on an attempt to insert a key outside of its range, or to construct a
 * submap either of whose endpoints lie outside its range.
 *
 * @param fromKey low endpoint of the keys in the returned map
 * @param fromInclusive {@code true} if the low endpoint
 *        is to be included in the returned view
 * @param toKey high endpoint of the keys in the returned map
 * @param toInclusive {@code true} if the high endpoint
 *        is to be included in the returned view
 * @return a view of the portion of this map whose keys range from
 *         {@code fromKey} to {@code toKey}
 * @throws ClassCastException if {@code fromKey} and {@code toKey}
 *         cannot be compared to one another using this map's comparator
 *         (or, if the map has no comparator, using natural ordering).
 *         Implementations may, but are not required to, throw this
 *         exception if {@code fromKey} or {@code toKey}
 *         cannot be compared to keys currently in the map.
 * @throws NullPointerException if {@code fromKey} or {@code toKey}
 *         is null and this map does not permit null keys
 * @throws IllegalArgumentException if {@code fromKey} is greater than
 *         {@code toKey}; or if this map itself has a restricted
 *         range, and {@code fromKey} or {@code toKey} lies
 *         outside the bounds of the range
 */
NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive,
                         K toKey,   boolean toInclusive);
/**
*返回此映射中关键帧范围为的部分的视图
*{@code fromKey}到{@code toKey}。如果{@code fromKey}和
*{@code toKey}相等,则返回的映射为空,除非
*{@code from exclusive}和{@code to exclusive}都是真的。这个
*返回的映射由该映射支持,因此返回的映射中的更改是
*反映在这张地图上,反之亦然。返回的映射支持所有
*此映射支持的可选映射操作。
*
*返回的映射将抛出{@code IllegalArgumentException}
*尝试将密钥插入其范围之外,或构造
*端点位于其范围之外的子贴图。
*
*@param fromKey返回映射中键的低端
*@param fromInclusive{@code true}如果低端
*将包含在返回的视图中
*@param toKey返回映射中键的高端
*@param toInclusive{@code true}如果高端
*将包含在返回的视图中
*@返回此地图中关键帧范围从
*{@code fromKey}到{@code toKey}
*@在{@code fromKey}和{@code toKey}时抛出ClassCastException
*无法使用此地图的比较器相互比较
*(或者,如果地图没有比较器,则使用自然排序)。
*实现可能(但不是必须)抛出此
*如果{@code fromKey}或{@code toKey}发生异常
*无法与地图中当前的关键点进行比较。
*@在{@code fromKey}或{@code toKey}时引发NullPointerException
*为null,此映射不允许null键
*如果{@code fromKey}大于,则@throws IllegalArgumentException
*{@code toKey};或者如果这张地图本身有一个
*范围,并且{@code fromKey}或{@code toKey}位于
*超出范围的
*/
NavigableMap子映射(K fromKey,boolean fromInclusive,
K toKey,布尔型(包括在内);

树映射的底层数据结构是红黑树,所有的复杂性都由NavigableMap接口抽象出来,因此使用起来非常简单。

我只希望您的
不是基于
Double
Float
。。。这将导致由于浮点运算问题而导致的故障。如果使用
java.util.NavigableMap
,请在文档中查找它,并检查它是否支持您所需的内容。