HowToDoInJava

  • All Tutorials
  • Spring Boot
  • Java 8
  • Interview Questions
Home / Core Java / Collections / Difference between enumerator and iterator?

Difference between enumerator and iterator?

October 20, 2015 by Lokesh Gupta

Difference between Enumerator and Iterator can be asked to you in any java interview. In this post, I am listing down few differences which you may cite while answering the question.

Difference between enumerator and iterator

First of all, enumerations are only applicable for legacy classes e.g Hashtable, Vector. Enumerations were part of initial java release JDK1.0. While, iterators were included in JDK 1.2 along with Collections framework which was also added in JDK 1.2 only.

So clearly, Iterators were designed as totally focused on collection framework only. If you read the Iterator’s java documentation, it clearly states it’s purpose. Quoting from oracle’s official website:

An iterator over a collection. Iterator takes the place of Enumeration in the Java Collections Framework. Iterators differ from enumerations in two ways:

  1. Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics.
  2. Method names have been improved.

This interface is a member of the Java Collections Framework.

The bottom line is, both Enumeration and Iterator will give successive elements, but Iterator is improved in such a way so the method names are shorter, and has an additional remove() method.

Here is a side-by-side comparison:

[su_table]

EnumerationIterator
hasMoreElement()hasNext()
nextElement()next()
N/Aremove()

[/su_table]

Java API Specifications recommend, for newer programs, Iterator should be preferred over Enumeration, as “Iterator takes the place of Enumeration in the Java collections framework.”

That’s all for this simple yet important topic.

Happy Learning !!

Stay Updated with Awesome Weekly Newsletter

Unsubscribe

About Lokesh Gupta

Founded HowToDoInJava.com in late 2012. I love computers, programming and solving problems everyday. A family guy with fun loving nature. You can find me on Facebook, Twitter and Google Plus.

Ask Questions & Share Feedback Cancel reply

Your email address will not be published. Required fields are marked *

*Want to Post Code Snippets or XML content? Please use [java] ... [/java] tags otherwise code may not appear partially or even fully. e.g.
[java] 
public static void main (String[] args) {
...
}
[/java]

Help me fight spammers. Solve this simple math. *

Search Tutorials

  • Email
  • Facebook
  • RSS
  • Twitter

Recommended

  • 10 Life Lessons
  • How to Start New Blog
  • Secure Hash Algorithms
  • Regular Expressions
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices
  • Java Interview Questions
  • Microservices Tutorial
  • REST API Tutorial

Collection Framework Tutorials

  • How HashMap Works?
  • Comparable and Comparator
  • Design Good Key for HashMap
  • HashMap vs. Hashtable
  • Enumerator vs Iterator
  • How Iterator Works?
  • ConcurrentHashMap
  • Print Array Content
  • Collection Interview Questions
  • ConcurrentHashMap Questions
  • Iterate over Collections
  • Iterate Over List
  • Sort Map by keys
  • Sort Map by values


Developer Tools

  • JSON Formatter and Minifier
  • XML Formatter and Minifier
  • CSS Formatter and Minifier
  • HTML Formatter and Minifier

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

References

  • Java 8 API
  • Spring Framework Reference
  • RESTEasy Reference
  • Hibernate User Guide
  • Junit Wiki
  • Maven FAQs

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. Site hosted on Bluehost

.