java – TreeMap中的键返回null
所以我有一个非常奇怪的错误.当我最初使用keySet()迭代大型TreeMap的前10个键时,我偶然发现了它.其中一个键是返回null,就我的理解而言,这是不可能的.所以我在下面写了测试代码:
int i = 0; for (Map.Entry<String,Integer> es : sortedMap.entrySet()){ if (i >= 10) { break; } if (sortedMap.containsKey(es.getKey())){ System.out.println(es.getKey() + ":" + sortedMap.get(es.getKey())); } else { System.out.println("Key " + es.getKey() + " does not exist,yet..."); System.out.println("This does work: " + es.getKey() + ":" + es.getValue()); System.out.println("This does NOT work: " + es.getKey() + ":" + sortedMap.get(es.getKey())); } i++; } 并获得以下结果: SOAP:967 'excerpt'::679 'type'::679 Key 'author_url': does not exist,yet... This does work: 'author_url'::679 This does NOT work: 'author_url'::null 'date'::679 Android:437 TLS:295 message:283 server:230 monthly:215 <<<<<<<<<<<<<<<<<<<<DUMPING MAP! {SOAP=967,'excerpt':=679,'type':=679,'author_url':=679,'date':=679,Android=437,TLS=295,message=283,server=230,monthly=215... 我在前十名之后切断了地图,因为那里有更多的地图,但所有这些都是一个有价值的钥匙. 所以我的问题是这样的:当我使用密钥从TreeMap直接获取(key)时,为什么我得到null,但是EntrySet返回正确的键和值? 这是我的比较器,因为我在Integer上订购: class ValueComparator implements Comparator<Object> { Map<String,Integer> base; public ValueComparator(Map<String,Integer> base) { this.base = base; } public int compare(Object a,Object b) { if ((Integer) base.get(a) < (Integer) base.get(b)) { return 1; } else if ((Integer) base.get(a) == (Integer) base.get(b)) { return 0; } else { return -1; } } } TreeMap的构建如下: ValueComparator bvc = new ValueComparator(allMatches); TreeMap<String,Integer> sortedMap = new TreeMap<String,Integer>(bvc); //Sort the HashMap sortedMap.putAll(allMatches); allMatches是HashMap< String,Integer> 解决方法
从TreeMap显示的迭代顺序可以看出,您使用的是自定义Comparator. [否则迭代将按字典顺序排列]
请注意,根据javadocs:
如果您的比较器不应用这些规则 – 行为未定义,可能会显示奇怪的结果 – 如您所见. 编辑:[作为对编辑问题的回应] (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |