What is the difference between Enumeration and Iterator ?
2013-11-17 21:43
495 查看
What are differences between Enumeration and Iterator This question is from early ages ofinterview , I have not seen
this question on recentinterviews but it was common during 2006-2007 , now days questions like implementation of HashMap, ConcurrentHashMap etc has take its
place, nonetheless its very useful to know fundamental difference between Iterator and Enumeration.Some time its also asked as Iterator vs Enumeration or Enumeration vs Iterator which is same. important point to note is that both Iterator
and Enumeration provides way to traverse or navigate through entire collection injava
Between Enumeration and Iterator, Enumeration is older and its there from JDK1.0 while iterator was introduced later. Iterator can be used withJava
arraylist,
java hashmap keyset and with any other collection classes.
Another
similarity between Iterator and Enumeration in Java is that functionality of Enumeration interface is duplicated by the Iterator interface.
Only major difference between Enumeration and iterator is Iterator has a remove()method while Enumeration doesn't. Enumeration acts as Read-only interface,
because it has themethods only to traverse and fetch the objects, where as by using Iterator we can manipulate the objects like adding and removing the objects
from collection e.g. Arraylist.
Also Iterator is more secure and safe as compared toEnumeration
because it does not allow other thread to modify the collection object while some thread is iterating over it and throwsConcurrentModificationException.
This is by far most important fact for me for deciding between Iterator vs Enumeration in Java.
In Summary both Enumeration and Iterator will give successive elements, but Iterator isnew and improved version where method names are
shorter, and has new method called remove. Here is a short comparison:
Enumeration
hasMoreElement()
nextElement()
N/A
Iterator
hasNext()
next()
remove()
So Enumeration is used when ever we want to make Collection objects as Read-only.
if you like to read interview questions you can alsocheck
Why String is immutable in Java
Top 20 core Java interview questions in Java
10 Java Singleton pattern interview questions
10 interview questions on java serialization
15 multi-threading interview questions asked in Investment banks
Read more: http://javarevisited.blogspot.com/2010/10/what-is-difference-between-enumeration.html#ixzz2ki1uv7nU
this question on recentinterviews but it was common during 2006-2007 , now days questions like implementation of HashMap, ConcurrentHashMap etc has take its
place, nonetheless its very useful to know fundamental difference between Iterator and Enumeration.Some time its also asked as Iterator vs Enumeration or Enumeration vs Iterator which is same. important point to note is that both Iterator
and Enumeration provides way to traverse or navigate through entire collection injava
Between Enumeration and Iterator, Enumeration is older and its there from JDK1.0 while iterator was introduced later. Iterator can be used withJava
arraylist,
java hashmap keyset and with any other collection classes.
Another
similarity between Iterator and Enumeration in Java is that functionality of Enumeration interface is duplicated by the Iterator interface.
Only major difference between Enumeration and iterator is Iterator has a remove()method while Enumeration doesn't. Enumeration acts as Read-only interface,
because it has themethods only to traverse and fetch the objects, where as by using Iterator we can manipulate the objects like adding and removing the objects
from collection e.g. Arraylist.
Also Iterator is more secure and safe as compared toEnumeration
because it does not allow other thread to modify the collection object while some thread is iterating over it and throwsConcurrentModificationException.
This is by far most important fact for me for deciding between Iterator vs Enumeration in Java.
In Summary both Enumeration and Iterator will give successive elements, but Iterator isnew and improved version where method names are
shorter, and has new method called remove. Here is a short comparison:
Enumeration
hasMoreElement()
nextElement()
N/A
Iterator
hasNext()
next()
remove()
So Enumeration is used when ever we want to make Collection objects as Read-only.
if you like to read interview questions you can alsocheck
Why String is immutable in Java
Top 20 core Java interview questions in Java
10 Java Singleton pattern interview questions
10 interview questions on java serialization
15 multi-threading interview questions asked in Investment banks
Read more: http://javarevisited.blogspot.com/2010/10/what-is-difference-between-enumeration.html#ixzz2ki1uv7nU
相关文章推荐
- Java - What is difference between Enumeration and Iterator?
- what is the difference between the setTimeout and setInterval
- what is the difference between json and javascript object.
- c - What is the Difference Between read() and recv() , and Between send() and write()? - Stack Overflow
- what is the difference between static and normal variables in c++
- What is the difference between Views and Materialized Views in Oracle?
- What is the difference between << and += while injecting elements to Hash.new([])?
- Whats is the difference between train, validation and test set, in neural networks?
- What is the difference between ++i and i++
- Design: What is the basic difference between precast and prefabricated structures?
- What is the difference between /dev/ttyUSB and /dev/ttyACM
- What is the difference between 0 dB and 0 dBFS
- What is the difference between extensibility and scalability?
- What is the difference between Terminal, Console, Shell, and Command Line?
- Q: What is the difference between protocol stacks and profiles?蓝牙协议栈和profile的区别
- What Is the Difference Between a Block, a Proc, and a Lambda in Ruby
- macro与inline的区别 What is the difference between macro and inline?
- What exactly is the difference between “pass by reference” in C and in C++?
- What is The Difference Between Risk Appetite, Risk Tolerance and Risk Threshold?
- what is the difference between zImage (kernel) and boot.img