Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distinction between iterator and enumerator

An interview question for a .NET 3.5 job is "What is the difference between an iterator and an enumerator"?

This is a core distinction to make, what with LINQ, etc.

Anyway, what is the difference? I can't seem to find a solid definition on the net. Make no mistake, I can find the meaning of the two terms but I get slightly different answers. What would be the best answer for an interview?

IMO an iterator "iterates" over a collection, and an enumerator provides the functionality to iterate, but this has to be called.

Also, using the yield keyword is said to save state. What exactly is this state? Is there an example of this benefit occurring?

like image 632
GurdeepS Avatar asked Apr 04 '09 01:04

GurdeepS


People also ask

What is difference between Iterator and enumerator?

Iterator can do modifications (e.g using remove() method it removes the element from the Collection during traversal). Enumeration interface acts as a read only interface, one can not do any modifications to Collection while traversing the elements of the Collection.

Which is faster Enumeration or Iterator?

Iterator is used to iterate most of the classes in the collection framework like ArrayList, HashSet, HashMap, LinkedList etc. Enumeration is fail-safe in nature. Iterator is fail-fast in nature. Enumeration is not safe and secured due to it's fail-safe nature.

What is difference between Iterator and Splititerator?

Iterator performs only iteration over a set of elements. But, Spliterator splits as well as iterates over a set of elements which is very useful in parallel processing of elements.

What is the difference between enum and Enumeration in Java?

Enumeration is created by using a keyword called “enum”. Given below is the syntax with which we can create an enumeration. Note: enum can be defined only inside a top-level class or interface or in a static context. It should not be inside a method.


2 Answers

Iterating means repeating some steps, while enumerating means going through all values in a collection of values. So enumerating usually requires some form of iteration.

In that way, enumerating is a special case of iterating where the step is getting a value from a collection.

Note the "usually" – enumerating may also be performed recursively, but recursion and iteration are so closely related that I would not care about this small difference.

You may also enumerate values you do not explicitly store in a collection. For example, you can enumerate the natural number, primes, or whatever but you would calculate these values during the enumeration and not retrieve them from a physical collection. You understand this case as enumerating a virtual collection with its values defined by some logic.


I assume Reed Copsey got the point. In C# there are two major ways to enumerate something.

  1. Implement Enumerable and a class implementing IEnumerator
  2. Implement an iterator with the yield statement

The first way is harder to implement and uses objects for enumerating. The second way is easier to implement and uses continuations.

like image 61
Daniel Brückner Avatar answered Oct 28 '22 11:10

Daniel Brückner


In C# 2+, iterators are a way for the compiler to automatically generate the IEnumerable and/or IEnumerable<T> interfaces for you.

Without iterators, you would need to create a class implementing IEnumerator, including Current, MoveNext, and Reset. This requires a fair amount of work. Normally, you would create a private class that implemtented IEnumerator<T> for your type, then yourClass.GetEnumerator() would construct that private class, and return it.

Iterators are a way for the compiler to automatically generate this for you, using a simple syntax (yield). This lets you implement GetEnumerator() directly in your class, without a second class (The IEnumerator) being specified by you. The construction of that class, with all of its members, is done for you.

Iterators are very developer friendly - things are done in a very efficient way, with much less effort.

When you use foreach, the two will behave identically (provided you write your custom IEnumerator correctly). Iterators just make life much simpler.

like image 45
Reed Copsey Avatar answered Oct 28 '22 10:10

Reed Copsey