java 检索 HashMap 的迭代器会引发强制转换异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5062057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieving an iterator for a HashMap throws a cast exception
提问by Mack
I have some relatively simple code I have made that is supposed to retrieve an iterator to a HashMap & print out the key pair values.
我有一些相对简单的代码,我应该将迭代器检索到 HashMap 并打印出键对值。
My problem is that when I go to retrieve the iterator I get this exception thrown.
我的问题是,当我去检索迭代器时,我抛出了这个异常。
Exception in thread "main" java.lang.ClassCastException: java.util.HashMap$Entry cannot be cast to java.lang.String
at Delete.main(Delete.java:25)
线程“main”中的异常 java.lang.ClassCastException:java.util.HashMap$Entry 无法
在 Delete.main(Delete.java:25) 处强制转换为 java.lang.String
Here is my code & I point out where line 25 is:
这是我的代码,我指出第 25 行的位置:
Map <String, UpdatablePage> contentMap = new HashMap <String, UpdatablePage>();
contentMap.put( "test", new UpdatablePage() );
for ( Iterator it = (Iterator) contentMap.entrySet().iterator(); it.hasNext(); )
{
String key = (String) it.next(); // LINE 25
UpdatablePage value = contentMap.get(key);
System.out.print( "Key=" + key + ", Value=" + value.toString() );
}
PS, I have tried to iterate over a map using the Map.Entry but when I go to cast the value returned by the iterator to a Map.Entry I get another casting exception, why?
PS,我曾尝试使用 Map.Entry 遍历地图,但是当我将迭代器返回的值转换为 Map.Entry 时,我又遇到了另一个转换异常,为什么?
// Error occurs when I do the following
Map.Entry pair = (Map.Entry) it.next();
采纳答案by Kaffiene
In this line: for ( Iterator it = (Iterator) contentMap.entrySet().iterator(); it.hasNext(); )
在这一行中: for ( Iterator it = (Iterator) contentMap.entrySet().iterator(); it.hasNext(); )
entrySet() should be keySet() - your iterator returns each entry but as you're casting them to String, I presume it's the keys you want.
entrySet() 应该是 keySet() - 您的迭代器返回每个条目,但是当您将它们转换为 String 时,我认为它是您想要的键。
回答by Lawrence Dol
When iterating the entry set you get back Map.Entry
objects, on which you can then use getKey()
and getValue()
to extract the key and value. No need to iterate the keys and separately extract the value (which is much less efficient).
迭代条目集时,您会返回Map.Entry
对象,然后您可以在其上使用getKey()
和getValue()
提取键和值。无需迭代键并单独提取值(效率低得多)。
By the way, you would not cast the iteratorto Map.Entry, but the object which the iterator returns on each iteration.
顺便说一下,您不会将迭代器转换为 Map.Entry,而是将迭代器在每次迭代时返回的对象。
回答by Hovercraft Full Of Eels
Use keySet() and a generic iterator for best results:
使用 keySet() 和通用迭代器以获得最佳结果:
Map<String, UpdatablePage> contentMap = new HashMap<String, UpdatablePage>();
contentMap.put("test", new UpdatablePage());
for (Iterator<String> it = contentMap.keySet().iterator(); it.hasNext();) {
String key = it.next();
UpdatablePage value = contentMap.get(key);
System.out.print("Key=" + key + ", Value=" + value.toString());
}
With the generic iterator, there's no need for any casts at all, and you get the benefit of compile-time type checking rather than runtime exception as you're seeing.
使用泛型迭代器,根本不需要任何类型转换,您将获得编译时类型检查的好处,而不是您所看到的运行时异常。