JAVA系列: Iterator和Enumeration

阅读 222

2022-02-10



Iterator

Iterator遍历Collection时,是fail-fast机制的。


即,当某一个线程A通过iterator去遍历某集合的过程中,若该集合的内容被其他线程所改变了;那么线程A访问集合时,就会抛出ConcurrentModificationException异常,产生fail-fast事件。



Enumeration 比 Iterator 的遍历速度更快

从中,我们可以看出。Enumeration 比 Iterator 的遍历速度更快。为什么呢?

这是因为,Hashtable中Iterator是通过Enumeration去实现的,而且Iterator添加了对fail-fast机制的支持;所以,执行的操作自然要多一些。


精彩评论(0)

0 0 举报