Monday, June 22, 2015

Iterator v/s Enumator

Iterator v/s Enumerator

Often asked during interviews from Collections. They are both used to iterate over a Collection of Data. Below is the difference in tabular format and sufficient for interviews.


Differences
S No.
Iterator
Enumerator
1
Methods
hasNext()
next()
remove()
Methods
hasMoreElement()
nextElement()
2
Secure as throws ConcurrentModificationException if other thread tries to modify the Collection object while one thread is iterating over it
Not secure as no concept of ConcurrentModificationException
3
remove() method makes it read/modify interface
remove() method is absent, hence it is Read-only interface




Similarities
S No.
Iterator
Enumerator
1
Interface
Interface
2
java.util package
java.util package
3
Used for iteration
Used for iteration

No comments:

Post a Comment