Iterator vs enumeration in java
Here we will learn about major differences between:
Iterator vs Enumeration differ based on the following parameters:
Enumeration was introduced as part of JDK1.0
Iterator was introduced from JDK1.2
2. Modifying Collection
Enumeration cannot be used to modify the elements in Collection while traversing.
Iterator can be used to remove the element in Collection while traversing (it can remove element using remove() )
3. Available methods
Enumeration: hasMoreElements(), nextElement()
Iterator: hasNext(), next(), remove()
4. Fail fast or Fail safe
Enumeration is fail safe by nature.
Iterator is fail fast by nature.
Iterator being fail fast by nature is considered safe than enumeration as it throws ConcurrentModificationException, if any other thread tries to modify the Collection that is being traversed by Iterator.
5. Legacy concept
Enumeration is generally preferred for traversing over legacy classes like Vector, HashTable, etc.
Iterator is used to traverse over almost all classes in java Collection framework like ArrayList, LinkedList, HashMap, HashSet, etc.
Iterator is preferred over Enumeration.
Would you like to see your article here on tutorialsinhand.
program by tutorialsinhand.com
About the Author
Technology geek, loves to write and share knowledge with the world. Having 9+ years of IT experience. B.Tech in Computer Science & Engineering
Page Views :
Published Date :