Wednesday
Nov 22 2006
Java: Iterators are not Iterable
This is something I stumbled across multiple times now in Java: Iterators are not Iterable. Java 1.5 introduced the foreach statement, which allows easy iteration over collections like this:
for (MyClass item: myCollection) { doStuffWithItem(item); }
For this to work class MyClass must implement the Iterable interface. This interface defines just one method that returns an Iterator:
Iterator<T> iterator();
This works fine in many cases. But now suppose we have a class that can be iterated in multiple ways. For example a Tree class that can be iterated breadth first or depth first. I would define two methods and use them like this:
class Tree { Iterator<Node> depthFirstIterator(); Iterator<Node> breadthFirstIterator(); } // ... for (Node node: myTree.depthFirstIterator()) { }
This won't work in Java. Iterators do not extend the Iterable interface, like they in other languages. For example in Python an iterator will return itself when its iterator() equivalent is called. Instead in Java I have to return an object that itself implements the Iterable interface like this:
class IterableIterator<T> implements Iterable<T> { private Iterator<T> iter; public IterableIterator(Iterator<T> iter) { this.iter = iter; } public Iterator<T> iterator() { return iter; } } class Tree { IterableIterator<Node> depthFirstIterator(); IterableIterator<Node> breadthFirstIterator(); }
Not only is this boilerplate code for no gain, a generic class like the IterableIterator above is not included in Java's standard library.
Comments
by magstuar
Java don't have expresion like "yiel return" very usefull
in lenguages like CLU and C#
Nice site
by Valintino
Hello, Your site is great. Regards, Valintino Guxxi
good day to all at www.rittau.org
by JoJo Jacky
Hey
I i saw the msg on www.rittau.org
Very absorbing
In fact I have been looking for this for months
www.rittau.org is a site i'll keep bookmarked
Great effort well done !
John
<a href=http://www.adstractor.com/?p=296> tractor plowing </a>
ho to all at www.rittau.org
by JoJo John Jnr
Hello there
I saw yer thread on www.rittau.org
Very interesting
In fact I have been searching for something like this for ages
www.rittau.org will see me back again
Great effort well done !
John
<a href=http://www.only-cars.com/?p=406521> teen for car used best </a>
Excellent Farmville Fansite. Get Free cheats and bots for your farm
by Peabalensep
Hi members Are you addicted to Farmville? I’ve discovered this incredible site that has all the latest hints, tips, tricks and cheats here:
<a href=http://www.ultimatefarmville.com>Farmville Cheats</a> I was finding it difficult on Farmville and couldn’t understand how all my neighbors were doing so much better than me, but then I discovered this site and since then I’ve become a way greater farmer. The site has some Farmville cheats that I haven’t found anywhere else and has a great store where you can get the most up to date bots for Farmville. I’ve also discovered they have started competitions where you can win the bots and stuff and it is completely free to enter. If you are hooked to Farmville like me then you need to visit this site <a href=http://www.ultimatefarmville.com>Ultimate Farmville</a>
Re
by gXRuby
Thanks for the information just about this post! In fact that the term paper writing services can serve the <a href=" http://www.exclusivepapers.com">essay writing</a>. Thus, that’s a very good chance to do essay buy and <a href=" http://www.exclusivepapers.com">custom writing</a> referring to this good post.