您对HashMap的迭代器是快速失败而HashTable的枚举器不是t到底是什么意思? [英] What do you exactly mean by HashMap's iterator is fail-fast and HashTable's enumerator isn't?

查看:103
本文介绍了您对HashMap的迭代器是快速失败而HashTable的枚举器不是t到底是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找两个类之间的区别,而这个博客的出处是很多答案. http://javarevisited.blogspot.com/2010/10/difference-between-hashmap-and.html

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?

感谢您的光临!

推荐答案

快速失败"是指在遍历内容时尝试修改内容时,它将失败并引发ConcurrentModificationException.

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
} 

对于HashTable枚举:

For HashTable enumeration:

 Enumeration keys = hashTable.keys();
 while (keys.hasMoreElements()) {
          hashTable.put(someKey, someValue);  //this is ok
    }

这篇关于您对HashMap的迭代器是快速失败而HashTable的枚举器不是t到底是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆