[ACCEPTED]-Can a collection have multiple iterators in Java?-iterator

Accepted answer
Score: 15

Yes.

Sometimes it's really annoying that 1 answers have to be 30 characters.

Score: 8

Yes, it is possible. That's one reason they 7 are iterators, and not simply methods of 6 the collection.

For example List iterators (defined 5 in AbstractList) hold an int to the current index (for 4 the iterator). If you create multiple iterators 3 and call next() a different number of times, each 2 of them will have its int cursor with a different 1 value.

Score: 1

Yes and no. That depend of the implementation 10 of the interface Iterable<T>.

Usually it should return 9 new instance of a class that implement Iterable 8 interface, the class AbstractList implements 7 this like that:

public Iterator<E> iterator() {
    return new Itr(); //Where Itr is an internal private class that implement Itrable<T>
}

If you are using standard 6 Java classes You may expect that this is 5 done this way.

Otherwise You can do a simple 4 test by calling iterator() form the object and then 3 run over first and after that second one, if 2 they are depend the second should not produce 1 any result. But this is very unlikely possible.

Score: 1

You could do something like this:

import java.util.ArrayList;
import java.util.Iterator;

public class Miterate {

    abstract class IteratorCaster<E> implements Iterable<E>, Iterator<E> {
        int mIteratorIndex = 0;

        public boolean hasNext() {
            return mStorage.size() > mIteratorIndex;
        }

        public void remove() {
        }

        public Iterator<E> iterator() {
            return this;
        }
    }

    class FloatCast extends IteratorCaster<Float> {
        public Float next() {
            Float tFloat = Float.parseFloat((String)mStorage.get(mIteratorIndex));
            mIteratorIndex ++;
            return tFloat;
        }
    }

    class StringCast extends IteratorCaster<String> {
        public String next() {
            String tString = (String)mStorage.get(mIteratorIndex);
            mIteratorIndex ++;
            return tString;
        }
    }

    class IntegerCast extends IteratorCaster<Integer> {
        public Integer next() {
            Integer tInteger = Integer.parseInt((String)mStorage.get(mIteratorIndex));
            mIteratorIndex ++;
            return tInteger;
        }
    }

    ArrayList<Object> mStorage;

    StringCast mSC;
    IntegerCast mIC;
    FloatCast mFC;

    Miterate() {
        mStorage = new ArrayList<Object>();

        mSC = new StringCast();
        mIC = new IntegerCast();
        mFC = new FloatCast();


        mStorage.add(new String("1"));
        mStorage.add(new String("2"));
        mStorage.add(new String("3"));
    }

    Iterable<String> getStringIterator() {
        return mSC;
    }

    Iterable<Integer> getIntegerIterator() {
        return mIC;
    }

    Iterable<Float> getFloatIterator() {
        return mFC;
    }

    public static void main(String[] args) {
        Miterate tMiterate = new Miterate();

        for (String tString : tMiterate.getStringIterator()) {
            System.out.println(tString);
        }

        for (Integer tInteger : tMiterate.getIntegerIterator()) {
            System.out.println(tInteger);
        }

        for (Float tFloat : tMiterate.getFloatIterator()) {
            System.out.println(tFloat);
        }
    }
}

0

Score: 0

With the concurrent collections you can 2 have multiple iterators in different threads 1 even if there inserts and deletes.

More Related questions