简体   繁体   English

TreeMap.putAll 给出的键超出范围

[英]TreeMap.putAll giving key out of range

In my code,在我的代码中,

            SortedMap<Integer, Data> subMap;

            subMap = (db.getDataMap()).tailMap(previousServer);

            SortedMap<Integer, Data>  temp = (db.getDataMap()).headMap(presentServer);
            //System.out.println(subMap);
            //System.out.println(temp);
            subMap.putAll(temp);

suppose tailMap() returns nothing, so subMap is an empty Map, but temp has few key-value pair.假设 tailMap() 什么都不返回,所以 subMap 是一个空 Map,但 temp 几乎没有键值对。 So adding temp to subMap via subMap.putAll(temp) is giving me "key out of range".因此,通过 subMap.putAll(temp) 将 temp 添加到 subMap 给了我“键超出范围”。 But if i do temp.putAll(subMap) then everything works fine.但是如果我做 temp.putAll(subMap) 那么一切正常。

What could be the reason here?这可能是什么原因?

SEE http://docs.oracle.com/javase/7/docs/api/java/util/SortedMap.html#tailMap(K) 请参阅http://docs.oracle.com/javase/7/docs/api/java/util/SortedMap.html#tailMap(K)

Note : several methods return submaps with restricted key ranges. 注意 :几种方法会返回键范围受限的子图。 Such ranges are half-open, that is, they include their low endpoint but not their high endpoint (where applicable). 这样的范围是半开的,也就是说,它们包括其低端点,但不包括其高端点(如果适用)。 If you need a closed range (which includes both endpoints), and the key type allows for calculation of the successor of a given key, merely request the subrange from lowEndpoint to successor(highEndpoint). 如果您需要一个封闭范围(包括两个端点),并且密钥类型允许计算给定密钥的后继者,则只需请求从lowEndpoint到后继者(highEndpoint)的子范围。 For example, suppose that m is a map whose keys are strings. 例如,假设m是一个键为字符串的映射。 The following idiom obtains a view containing all of the key-value mappings in m whose keys are between low and high, inclusive: 以下成语获取一个视图,该视图包含m中的所有键-值映射,其键在低端和高端之间,包括端值:

SortedMap<String, V> sub = m.subMap(low, high+"\0");

A similar technique can be used to generate an open range (which contains neither endpoint). 可以使用类似的技术来生成开放范围(不包含任何端点)。 The following idiom obtains a view containing all of the key-value mappings in m whose keys are between low and high, exclusive: 以下成语获取一个视图,该视图包含m中的所有键-值映射,其键在低和高之间(不包括):

SortedMap<String, V> sub = m.subMap(low+"\0", high);

来自文档的状态

IllegalArgumentException if fromKey is greater than toKey; or if this map itself has a restricted range, and fromKey or toKey lies outside the bounds of the range

I found this problem very awkward, as only soln I could find was to replace code in a function f() returning SortedMap:我发现这个问题很尴尬,因为我能找到的唯一解决方法是替换返回 SortedMap 的函数 f() 中的代码:

return docmap.headMap(nextkey);返回 docmap.headMap(nextkey);

where the returned map has a 'restricted range' (but there is no easy way to test this via a call that would be nice to have like map.isRangeRestricted())返回的地图有一个“受限范围”(但没有简单的方法可以通过像 map.isRangeRestricted() 这样的调用来测试它)

with code that copy constructs a new map based on the restricted-range map:使用复制代码基于受限范围地图构建新地图:

return new TreeMap(docmap.headMap(nextkey))返回新的 TreeMap(docmap.headMap(nextkey))

Otherwise the client code, which is making a call such as否则,正在调用的客户端代码,例如

mmap.putAll(f()), where mmap is non-empty (and in this case also has a restricted range as earlier output from f() also), barfs. mmap.putAll(f()),其中 mmap 是非空的(在这种情况下也有一个受限的范围,因为 f() 的早期输出也是如此),barfs。

I also find it counterintuitive that mmap can apparently have a restricted range but not mind doing a putAll as long as the map whose contents are being added does not have a restricted range as well.我还发现 mmap 显然可以具有受限范围但不介意执行 putAll 是违反直觉的,只要其内容被添加的地图也没有受限范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM