What Is Divergence Betwixt Enumeration In Addition To Iterator Inwards Java?

Though both Iterator in addition to Enumeration allows you lot to traverse over elements of Collections inward Java, at that spot is to a greater extent than or less pregnant departure betwixt them e.g. Iterator every bit good allows you lot to take away elements from collection during traversal but Enumeration doesn't allow that, it doesn't got the remove() method. Enumeration is every bit good a legacy aeroplane in addition to non all Collection supports it e.g. Vector supports Enumeration but ArrayList doesn't. On the other mitt Iterator is the measure aeroplane for iteration in addition to traversal. By the way,  what is departure betwixt Enumeration in addition to Iterator inward Java?  This enquiry is from early on ages of  Java interview , I possess got non seen this enquiry on recent interviews but it was quite mutual during 2000 to 2007 period, straight off days questions similar implementation of HashMap or ConcurrentHashMap etc has taken its place, withal its real of import to know the primal difference betwixt Iterator in addition to Enumeration. Some fourth dimension its every bit good asked every bit Iterator vs Enumeration or Enumeration vs Iterator which is same. Important betoken to Federal Reserve annotation is that both Iterator in addition to Enumeration provides agency to traverse or navigate through entire collection inward Java.

Iterator vs Enumeration

 Though both Iterator in addition to Enumeration allows you lot to traverse over elements of Collections  What is departure betwixt Enumeration in addition to Iterator inward Java?Between Enumeration in addition to Iterator, Enumeration is older in addition to its at that spot from JDK1.0, piece iterator was introduced later. Iterator tin last used amongst ArrayList, HashSet and other collection classes.  Another similarity betwixt Iterator in addition to Enumeration inward Java is that  functionality of Enumeration interface is duplicated past times the Iterator interface.


Only major departure betwixt Enumeration in addition to iterator is Iterator has a remove() method piece Enumeration doesn't. Enumeration acts every bit Read-only interface, because it has the methods alone to traverse in addition to fetch the objects, where every bit past times using Iterator nosotros tin manipulate the objects similar adding in addition to removing the objects from collection e.g. Arraylist.

Also Iterator is to a greater extent than secure in addition to prophylactic every bit compared to Enumeration because it  does non allow other thread to alter the collection object piece to a greater extent than or less thread is iterating over it in addition to throws ConcurrentModificationException. This is past times far nearly of import fact for me for deciding betwixt Iterator vs Enumeration inward Java.

In Summary both Enumeration in addition to Iterator volition order successive elements, but Iterator is novel in addition to improved version where method names are shorter, in addition to 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


Sumber https://javarevisited.blogspot.com/

0 Response to "What Is Divergence Betwixt Enumeration In Addition To Iterator Inwards Java?"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel