0

Possible Duplicate:
Difference between Java Enumeration and Iterator

I was reading this post "Difference between Iterator & Enumeration"

If can use either iterator methods or enumeration methods to perform the same action then what is the difference?

Where am I expected to use either of these interfaces?

Community
  • 1
  • 1
AppSensei
  • 8,270
  • 22
  • 71
  • 99
  • I don't get what you need that wasn't in the duplicate. The accepted answer seems clear and complete. Can you explain your problem ? – Denys Séguret Oct 03 '12 at 16:32
  • Does hasMoreElement() and hasNext() mean the same? – AppSensei Oct 03 '12 at 16:33
  • Yes. They're the same. One will save some precious octets on your hard disk. – Denys Séguret Oct 03 '12 at 16:33
  • 3
    Enumerators are a legacy Java 1.0 interface. Except for some APIs that still survive from then, you can pretend they don't exist. (Same goes for the old collection classes like `Vector` and `Hashtable`.) Use iterators or the enhanced for loop. – millimoose Oct 03 '12 at 16:33
  • Ok, It's preferred to use Iterators/Enhanced for-loops instead of Enumerations right? – AppSensei Oct 03 '12 at 16:35
  • 1
    @Appsheriff: Yes, they mean the same thing...but you can't use them interchangeably since one requires you to have an `Iterator` and the other requires you to have an `Enumeration`. Anyway, `Enumeration` has fallen out of use (it doesn't have language-level support like `Iterator` does). – Mark Peters Oct 03 '12 at 16:35
  • Thanks Guys, I got the answers I was looking for... – AppSensei Oct 03 '12 at 16:36

3 Answers3

4

Enumerators are part of legacy Java 1.0. Iterators only appeared in Java 1.2. To my knowledge, Enumerators are only kept for backward compatibility. As per the java docs for Enumerator, all new code should user the Iterator interface.

NOTE: The functionality of this interface is duplicated by the Iterator interface. In addition, Iterator adds an optional remove operation, and has shorter method names. New implementations should consider using Iterator in preference to Enumeration.

You should use Iterators when looping through a collection, list, set, etc or something that implements that Iterator interface. You can also use the "new" (Java 5) for loop construct to iterate over such a collection. Keep in mind, however, that the only safe way to remove an item from a collection when looping is to use the Iterator.remove() method.

Eric B.
  • 23,425
  • 50
  • 169
  • 316
3

Iterator is the newer, preferred method. It adds a remove function.

uberchris
  • 108
  • 5
1

Enumeration is used to get successive elements in lists, and cannot be used to alter the list in any way, such as through insertion or deletion. However, an iterator is an instantiated object that maintains a reference in the list, and can insert and delete elements at that reference, along with getting the elements in the list.
If you simply want to get the elements in a list, both enumeration and the use of an iterator will work fine. However, if you need the added function of insertion and deletion, I recommend using an iterator.

Tristan Hull
  • 360
  • 1
  • 3
  • 13