Articles

Iterator vs enumeration in java

Iterator vs enumeration in java

Here we will learn about major differences between:
  • iterator vs enumeration

Iterator vs Enumeration differ based on the following parameters:
 
1. Introduced
  • 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. 
 

Java Interview Questions

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Rohanjit Kumar
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 : Jun 28,2020  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!