What Is Deviation Betwixt Enumeration Together With Iterator Inward Java?
Though both Iterator too Enumeration allows you lot to traverse over elements of Collections inward Java, at that topographic point is only about pregnant departure betwixt them e.g. Iterator besides allows you lot to take elements from collection during traversal but Enumeration doesn't allow that, it doesn't got the remove() method. Enumeration is besides a legacy flat too non all Collection supports it e.g. Vector supports Enumeration but ArrayList doesn't. On the other mitt Iterator is the measure flat for iteration too traversal. By the way, what is departure betwixt Enumeration too Iterator inward Java? This inquiry is from early on ages of Java interview , I possess got non seen this inquiry on recent interviews but it was quite mutual during 2000 to 2007 period, immediately days questions similar implementation of HashMap or ConcurrentHashMap etc has taken its place, even too therefore its real of import to know the key difference betwixt Iterator too Enumeration. Some fourth dimension its besides asked every bit Iterator vs Enumeration or Enumeration vs Iterator which is same. Important betoken to banking enterprise complaint is that both Iterator too Enumeration provides agency to traverse or navigate through entire collection inward Java.
Iterator vs Enumeration
Between Enumeration too Iterator, Enumeration is older too its at that topographic point from JDK1.0, acre iterator was introduced later. Iterator tin dismiss last used alongside ArrayList, HashSet and other collection classes. Another similarity betwixt Iterator too Enumeration inward Java is that functionality of Enumeration interface is duplicated yesteryear the Iterator interface.
Only major departure betwixt Enumeration too iterator is Iterator has a remove() method acre Enumeration doesn't. Enumeration acts every bit Read-only interface, because it has the methods exclusively to traverse too fetch the objects, where every bit yesteryear using Iterator nosotros tin dismiss manipulate the objects similar adding too removing the objects from collection e.g. Arraylist.
Also Iterator is to a greater extent than secure too security every bit compared to Enumeration because it does non allow other thread to alter the collection object acre only about thread is iterating over it too throws ConcurrentModificationException. This is yesteryear far almost of import fact for me for deciding betwixt Iterator vs Enumeration inward Java.
In Summary both Enumeration too Iterator volition hand successive elements, but Iterator is novel too improved version where method names are shorter, too has novel method called remove. Here is a brusque comparison:
Enumeration
hasMoreElement()
nextElement()
N/A
Iterator
hasNext()
next()
remove()
So Enumeration is used when e'er nosotros desire to brand Collection objects every bit Read-only.
Further Learning
Java In-Depth: Become a Complete Java Engineer
Why String is immutable inward Java
Further Learning
Java In-Depth: Become a Complete Java Engineer
Why String is immutable inward Java
0 Response to "What Is Deviation Betwixt Enumeration Together With Iterator Inward Java?"
Post a Comment