Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Iterator vs C# IEnumerable

Tags:

java

iterator

c#

I've noticed that Java's Iterator (interface) is similar to C#'s IEnumerable, but is there any way to use it like this:

private IEnumerable<Label> it; 
it = labels.iterator();

In Java I could just do:

private Iterator<JLabel> it;
it = labels.iterator();

What's the C# equivalent of Java's Iterator interface?

like image 624
João Silva Avatar asked Nov 15 '13 00:11

João Silva


People also ask

Is iterator faster than for loop?

Iterator and for-each loop are faster than simple for loop for collections with no random access, while in collections which allows random access there is no performance change with for-each loop/for loop/iterator.

Why would you use an iterator Java?

Generally, an iterator in Java is used to loop through any collection of objects. To apply the iterator, all you need to do is import the java. util package and then use the iterator() method. You can then use the iterator to perform multiple operations in the collection.

Does C have iterator?

An iterator is an object that allows you to step through the contents of another object, by providing convenient operations for getting the first element, testing when you are done, and getting the next element if you are not. In C, we try to design iterators to have operations that fit well in the top of a for loop.

Are iterators faster?

To determine whether to use loops or iterators, you need to know which implementation is faster: the version of the search function with an explicit for loop or the version with iterators. The iterator version was slightly faster!


1 Answers

It's not used very often, but the analogy is the IEnumerator<T> interface:

var enumerator = labels.GetEnumerator();

.NET's IEnumerator differs from Java's Iterator with the following:

  • Iterator after construction is pointing at the first element of the collection (or, for an empty collection, is invalid and hasNext will return false immediately), IEnumerator points initially before the first element of the collection (for an empty collection MoveNext will return false)
  • Iterator has hasNext method, while for IEnumerator you verify the result of MoveNext method
  • Iterator has next method, while for IEnumerator you also use MoveNext
  • Iterator's next returns the next element, while with IEnumerator you use Current property after calling MoveNext
  • Iterator in Java has remove method which allows you to remove elements from the underlying collection. There is no equivalent in IEnumerator

So for Java you'd iterate with something like this:

it = labels.iterator();
while (it.hasNext())
{
    elem = it.next();
}

While in C#:

en = labels.GetEnumerator();
while (en.MoveNext())
{
    elem = en.Current;
}

Usually, having labels as a collection (which always implements IEnumerable<T>) you just use it directly:

foreach (var label in labels)
{
    //...
}

And of course, you can store IEnumerable<T> for later use (names referring to your example):

IEnumerable<Label> it = labels;

Beware, that IEnumerable<T> is lazy, just like Iterator in Java.

You can also easily obtain a snapshot of a collection like this (again, it refers to your example, better name could be chosen):

IEnumerable<Label> it = labels.ToArray();
// or Label[] it = labels.ToArray();
// or better: var it = labels.ToArray();
like image 174
BartoszKP Avatar answered Sep 22 '22 16:09

BartoszKP