Difference between Java Enumeration and Iterator

后端 未结 10 1727
庸人自扰
庸人自扰 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 10:06

    The main different is Enumeration doesn't expose remove() method. Moreover, Iterator don't allow a simultaneously navigation and modification on an underlying object. They have a control to see if there are concurrent modifications or so, and hence takes more processing. So Enumeration's performance is virtually 50% faster than Iterator. If we need only navigation ignoring such a synchronization, just use Enumeration.

    0 讨论(0)
  • 2020-11-27 10:10

    Both iterator and enumeration are used to retrieve the data, the difference is that enumeration can be used only for legacy classes i.e vector/stack whereas iterators can be used for the rest. Enumeration can also be used for the key set in maps.

    0 讨论(0)
  • Enumeration can be used only for the legacy class(Vector, Stack...), while Iterator can be used for all.

    0 讨论(0)
  • 2020-11-27 10:23

    One simple fact but haven't mentioned in previous answers is that Iterator<T> is used with Iterable<T> to serve in interpreting for(_type_ element:collection){...} structure.

    0 讨论(0)
提交回复
热议问题