• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Iterator & Enumeration

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

what is the difference between

Iterator & Enumeration..

Please........
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ragavendran Sivaji Rao wrote:
what is the difference between

Iterator & Enumeration..

Please........






In Java 5 and above you have use foreach loop on anything that implements Iterable:

for (Object o : list) {
do(o);
}

You can iterate over an Enumeration even it does not implement Iterable.

Iterable is a factory method for Iterator. Enumeration is similar to Iterator, and only maintains state for a single enumeration. So, be careful trying to wrap an Enumeration as an Iterable. If someone passes me an Iterable, I will assume that I can repeatedly call iterator on it, creating as many Iterators as I want, and iterating independently on each. A wrapped Enumeration will not fulfill this contract; don't let your wrapped Enumeration escape from your own code.
Enumeration is like an Iterator, not an Iterable. A Collection is Iterable. An Iterator is not.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you read the API documentation for Enumeration and Iterator?
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two major differences between the Enumeration and Iterator interfaces:
- Iterator has a remove() method
- the names of the methods in Iterator are a lot shorter

For the rest, they are quite the same, also in use: while there are more elements, get the next element and handle it. In code:
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic