Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Difference Between Iterator and Enumeration In Java

What is the difference between Iterator and Enumeration in Java is one of the oldest core Java Interview Questions. While I haven't seen this question for a long time, I still think its an important concept to know and remember for Java developers, especially those who are tasked to work in existing project which may be using Enumeration. The Iterator and Enumeration are two interfaces in Java and used for traversing in java collection we found these two interface in the java.util package .whenever we go for an interview if interviewer goes in collection topic he will often ask the difference between this two .so let's see one by one difference between this two and compare which is used when and which one is better.

Enumeration:

1. The Enumeration is an older interface found in JDK 1.0.

2.The Enumeration Interface does not have remove() method to delete the current element during traversing. 

3. The Enumeration  is a read-only interface we can only navigate in a collection using this interface
And only get objects if needed.

4. Enumerator interface is not thread-safe.

5. Enumeration is only applicable for legacy classes like Hashtable, Vector. All modern classes like ArrayList, LinkedList they all use Iterator. 

6. The Enumerations returned by the methods of classes like Hashtable, Vector is not fail-fast that is achieved by synchronizing the block of code inside the nextElement() method that locks the current Vector object which costs lots of time.

7. Enumerator has following methods




Iterator:

1. An Iterator is an improved version of Enumerator. This is also the standard class to traverse over Collection classes in Java. 

2. Iterator interface has remove() method, which can be used to delete the current element while Iterator. Make sure you use this remove method rather than Collection remove() method to avoid throwing ConcurrentModificationException in Java. 

3. An iterator is used for reading as well as for manipulating objects in the collection we can add or remove using an iterator.

4. An iterator is thread safe and secure, it does not allow other thread to modify the collection when one thread is doing iteration and throw ConcurrentModificationException.

5. An iterator is applicable for new java collection classes like ArrayList, HashMap.

6. Iterators are fail-fast if it fails for iterating it throws ConcurrentModificationException.

7. Iterator has comparatively shorter method names which helps in typing and improve readability. For example comparing to nextElement() from Enumeration, Iterator has just next() method. 

Difference Between Iterator and Enumeration In Java



That's all about difference between Iterator and Enumeration in Java. This is quite an important concept to remember as their iteration logic are also different. Enumeration is also a legacy class and there is no reason to use Enumeration anymore. You should use Iterator wherever possible. It's also make sense to replace Enumeration with Iterator in legacy code if you are allowed to, otherwise, there is not much benefit in touching something which is already working. 

All the best for your Java Interviews. You can also explore and find more Java interview questions using labels. I have shared many questions and answers here in past 10 to 12 years. 


No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.