HashMap的迭代器究竟意味着什么,它的失效速度很快,而HashTable的枚举器不是? [英] What do you exactly mean by HashMap's iterator is fail-fast and HashTable's enumerator isn't?
问题描述
我正在查找这两个班级之间的差异,这一点出现在很多答案中,这个博客是来源:
http://javarevisited.blogspot.com/2010/10/difference-between-hashmap-and.html
但是我并不完全明白。
有人可以详细说明这一点吗?也许有例子吗?
感谢您的支持!
快速失败意味着当您在迭代时尝试修改内容时,它会失败并抛出ConcurrentModificationException。 Set keys = hashMap.keySet();
for(Object key:keys){
hashMap.put(someObject,someValue); //它会在这里抛出ConcurrentModificationException
对于HashTable枚举:
枚举键= hashTable.keys();
while(keys.hasMoreElements()){
hashTable.put(someKey,someValue); //这是好的
}
I was looking up the difference between the two classes and this point came up in a lot of the answers with this blog being the source:
http://javarevisited.blogspot.com/2010/10/difference-between-hashmap-and.html
However I don't completely get it.
Can someone elaborate this? Perhaps with an example?
Thanks for looking in!
解决方案 Fail-fast means when you try to modify the content when you are iterating thru it, it will fail and throw ConcurrentModificationException.
Set keys = hashMap.keySet();
for (Object key : keys) {
hashMap.put(someObject, someValue); //it will throw the ConcurrentModificationException here
}
For HashTable enumeration:
Enumeration keys = hashTable.keys();
while (keys.hasMoreElements()) {
hashTable.put(someKey, someValue); //this is ok
}
这篇关于HashMap的迭代器究竟意味着什么,它的失效速度很快,而HashTable的枚举器不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!