Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Collection API

 
ravichandra Nibbaragandlla
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hai friends
can anyone help me understanding the difference between Enumuration and Iterator?

 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Enumeration and Iterator gives you almost same facility.

What Ivor Horton author of Beginning Java 2 says in his book regarding that:


An enumerator provides essentially the same capability as an iterator, but it
is recommended in the Java documentation that you use an iterator in
preference to an enumerator for collections. There is nothing particularly
wrong with enumerator- it's just that Iterator<> interface declares and
optional remove() method that the Enumeration<> interface does not, and the
methods in the Iterator<> interface have shorter names than those in the Enumeration<> interface, so code that uses them will be less cluttered.



Thanks,
 
Pravin Jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Enumeration and Iterator are both interfaces.
Enumeration is a legacy interface and is more
generalized, you may find it anywhere in the
entire api. Though it is commonly used with
the legacy classes like the Vector and Hashtable
it is also used independent of them also. eg.
StringTokenizer.
Iterator on the other hand is always used with
a Collection, it always has an underlying Collection
on which it operates. Also Iterator has an additional
functionality which allows removal of element from
the underlying Collection.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you really, really need an Enumeration for a Collection you can use the static method Collections.enumerator to give you one. The Enumeration is dead, long live Enumerations!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic