Sebastian Rittau's Blog

Wednesday
Nov 22 2006

Java: Iterators are not Iterable

Java, Programming

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.

Hi, all

by Carmelo Robertson

Do you have copy writer for so good articles? If so please give me contacts, because this really rocks! :)

Greetings!

by Allan Allen

Your blog keeps getting better and better! Your older articles are not as good as newer ones you have a lot more creativity and originality now. Keep it up!
And according to this article, I totally agree with your opinion, but only this time! :)

Click here! :)

by LongBeach

No matter what others say, I think it is still interesting and useful maybe necessary to improve some minor things

Free Wp Themes

by WP Themes

Nice post and this mail helped me alot in my college assignement. Gratefulness you for your information.

free food recipes

by food recipes

I absolutely agree with every word the author has written in this article!

bestmobcontent

by bestmobcontent

У Вас хороший ресурс, а у меня всё не хватает времени за своим следить(((

programing company

by virtual private servers

Hey hows it going Al! I was just wondering on your comment about C++0x extending C++.

by Alex

Hello, thank you very much for posting the piece of useful information. I’d like to study Java but I’m just a novice, that’s why I collect a lot of posts concerning it. It was interesting to know about Iterators. I’ll use the post for my essay writing.

Hello, thank you very much

by Alex271

Hello, thank you very much for posting the piece of useful information. I’d like to study Java but I’m just a novice, that’s why I collect a lot of posts concerning it. It was interesting to know about Iterators. I’ll use the post for my essay writing.

Denaq battery

by DENAQ battery

When was the last time you were driving down the digital highway and felt like you were shot by words?

Unbelievable

by Axel

Hi,

I have spent 30 min. to resolve an issue in my code, then I found your posting. I cannot believe that an Iterator object is not Iterable, I already tried a cast:

for(MyClass singleObj:(Iterable<MyClass>)collection.iterator)

But if it does not implement the interface, the cast will fail, right? I also get a compiler warning for a narrowing cast...

Axel

cool sunglasses

by sunglasses shop

Great article, i

hope can know much information About it!

Christian Louboutin Shoes

by Christian Louboutin

One again, your idea is very

good.thank you!very much.

by Spam, Spam, Spam, Spam!

Dude, you really need to get a hold of the spammers on your blog. The comments are about 80-90% spam.

very very very nice blog

by loan

www.rittau.org is very the most interesting. I enjoyed your blog a lot. Thank you.

Term Papers

by Term Papers

This is an astonishing entry. Thank you very much for the supreme post provided! I was looking for this entry for a long time, but I wasn’t able to find a trusted source.

Term Papers

by Term Papers

This is an astonishing entry. Thank you very much for the supreme post provided! I was looking for this entry for a long time, but I wasn’t able to find a trusted source.

Term Papers

by Term Papers

This is an astonishing entry. Thank you very much for the supreme post provided! I was looking for this entry for a long time, but I wasn’t able to find a trusted source.

Term Papers

by Term Papers

This is an astonishing entry. Thank you very much for the supreme post provided! I was looking for this entry for a long time, but I wasn’t able to find a trusted source.

Term Papers

by Term Papers

This is an astonishing entry. Thank you very much for the supreme post provided! I was looking for this entry for a long time, but I wasn’t able to find a trusted source.

cheap mbt

by mbt shoes

Very good sharing this.

registry cleaner software

by best registry cleaner

Great article, thank you very much!