Difference between Java Enumeration and Iterator

后端 未结 10 1807
庸人自扰
庸人自扰 2020-11-27 09:47

What is the exact difference between these two interfaces? Does Enumeration have benefits over using Iterator? If anyone could elaborate, a reference article would be appre

10条回答
  •  庸人自扰
    2020-11-27 09:58

    1) The main difference between Iterator and Enumeration is removal of the element while traversing the collection. Iterator can remove the element during traversal of collection as it has remove() method. Enumeration does not have remove() method.

    2) Enumeration is fail-safe in nature. It does not throw ConcurrentModificationException if Collection is modified during the traversal. Iterator is fail-fast in nature. It throws ConcurrentModificationException if a Collection is modified while iterating other than its own remove() method.

    3) Enumeration is a legacy interface which is used for traversing Vector, Hashtable. Iterator is not a legacy interface. Iterator can be used for the traversal of HashMap, LinkedList, ArrayList, HashSet, TreeMap, TreeSet .

提交回复
热议问题