views:

389

answers:

6

Could someone give me an example of the best way to add an iterator to a custom class pre Java 5's iterable interface?

Would wrapping a Collection be the best option? Something like

public Iterator iterator() {
    return wrappedCollection.iterator();
}

In my initial post I confused Iterator with Iterable. The end result I am interested in is being able to operate on the custom class with the following.

Iterator it = customClass.iterator();
while (it.hasNext()) {
    //do stuff
}
+4  A: 

Did you add a method that looks like

public Iterator iterator() {
    return new YourCustomIterator(...);
}

?

matt b
Or simply `return this;` if his object implements Iterator.
Michael Myers
Oh, yeah duh, the Iterator interface is for the actual iterator itself. I was so focused on recreating iterable I confused myself.
James McMahon
But come to think of it, you really, really don't want your collection to be the iterator. Then it would only work the first time you iterated through it.
Michael Myers
So I should be able to do iterator() { return collection.iterator(); }, with no ill side effects right?
James McMahon
@nemo: if you're just wrapping a collection, then yes, that would be the way to do it.
Michael Myers
+1  A: 

It sounds like you just added those methods your class that contains the Collection. In that case, your object is now also your iterator. What you probably meant to do is create a new class that implements Iterator and is instantiated with a clone of your Collection via an iterator() method.

jiggy
+1  A: 

If you just want to expose the iterator of an internal collection you just return that collections iterator. If you want to do more than that, you either have to wrap your own or check out the Apache Commons Collections package which includes quite a few iterator bulider and modifier methods. (Even if you can't use their methods, their implementations should teach you a lot about building iterators.)

Kathy Van Stone
+1  A: 

You want the collection to have an iterator method that returns an instance of a private Iterator class as needed.

public class IterableTest
{
    public Iterator iterator()
    {
        return new IteratorTest();
    }
    private class IteratorTest implements Iterator
    {
        public boolean hasNext(){...}

        public Object next(){...}

        public void remove(){...}
    }
}
Matthew Flaschen
Iterable is Java 5 or later.
James McMahon
Point taken. But the idea of the inner class remains.
Matthew Flaschen
+4  A: 

If you're just wrapping a collection you can use a forwarding method.

public class Custom implements Collection {
    Collection c; // create an instance here or in the constructor
    ...

    // forwarding method
    public Iterator iterator()
    {
        return c.iterator();
    }
}

I think it would be better to implement whatever type of Collection Interface you're wrapping, though, instead of Iterator.

Bill the Lizard
Why implement Iterator, though?
Michael Myers
@mmyers: The original question (now edited) led me down that path. I don't think that's a good idea, as I said in the edit I was making while you were writing your comment.
Bill the Lizard
I was so confused that I confused Bill.
James McMahon
Yeah, I read the original question. I just would have started the answer by saying what you said in your edit (so people wouldn't misunderstand it). :)
Michael Myers
I didn't think of that until I had submitted my answer and read over it a couple of times. :)
Bill the Lizard
Its been a loooong week.
James McMahon
@nemo: Nice job, but that's not that hard to do at 3PM on a Friday. :)
Bill the Lizard
A: 

I guess you are looking for something like below

http://karephul.blogspot.com/2009/05/concurrentmodificationexception.html

& yes, idea of inner class is what you are looking for.