Differences and Similarities between Iterator and Enumeration in java


In this Collection framework tutorial we will learn what are differences and similarities between java.util.Iterator and java.util.Enumeration in java.



Enumeration and iterator iterates over a collection.
Iterator was introduced to replace Enumeration in the Java Collections Framework.


Differences between java.util.Iterator and java.util.Enumeration in java >

Property
java.util.Enumeration
java.util.Iterator
1
Remove elements during iteration
java.util.Enumeration doesn’t allows to remove elements from collection during iteration in java.
java.util.Iterator allows to remove elements from collection during iteration by using remove() method in java.
2
Improved naming conventions in Iterator
nextElement()
Method Returns the next element of this enumeration if this enumeration object has at least one more element to provide.

hasMoreElements()
returns true if enumeration contains more elements.
nextElement() has been changed to next() in Iterator.

And

hasMoreElements() has been changed to hasNext() in Iterator.
3
Introduced in
which java
version
Enumeration was introduced in first version
of java i.e. ​JDK 1.0
Iterator was introduced in second version
of java i.e. ​JDK 2.0

Iterator was introduced to replace Enumeration in the Java Collections Framework.
4
Recommendation
Java docs recommends iterator over enumeration.
Java docs recommends iterator over enumeration.
5
Enumeration and Iterator over Vector
Enumeration returned by Vector is fail-safe, means any modification made to Vector during iteration using Enumeration don’t throw any exception in java.
Iterator returned by Vector are fail-fast, means any structural modification made to ArrayList during iteration will throw ConcurrentModificationException  in java.




So far we have learned what are differences between java.util.Iterator and java.util.Enumeration in java.
Now we will learn similarities between java.util.Iterator and java.util.Enumeration in Collection framework in java.



/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */


Similarities between java.util.Iterator and java.util.Enumeration in java

Property
java.util.Enumeration and
java.util.Iterator
1
iterates over a collection
Enumeration and iterator iterates over a collection in java.
2
Enumeration and iterator  both provides fail-safe and fail-fast iteration in java.



So in this Collection framework tutorial we will learn what are differences and similarities between java.util.Iterator and java.util.Enumeration in java.


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>

Comparable vs Comparator - differences and sorting list by implementing Comparable and Comparator in classes and inner classes in java


ArrayList vs LinkedList - Similarity and Differences

HashSet vs LinkedHashSet vs TreeSet - Similarity and Differences

Iterator vs ListIterator - Similarity and Differences in java

Collection vs Collections in java - Differences


Basic Collection - All properties in tabular form in java >

Collection - List, Set and Map all properties in tabular form in java



No comments:

Post a Comment