Can a collection have multiple iterators in Java?

Is it possible to have several iterators in one collection and track each of them independently? This does not imply deletion or insertion after assigning iterators.

+4
source share
5 answers

Yes.

Sometimes it is really annoying that the answers must be 30 characters long.

+10
source

Yes it is possible. This is one of the reasons they are iterators, and not just collection methods.

For example, List iterators (defined in AbstractList ) contain an int for the current index (for an iterator). If you create several iterators and call next() different number of times, each of them will have its own int cursor with a different value.

+6
source

Yes and no. It depends on the implementation of the Iterable<T> interface.

Usually it should return a new instance of the class that implements the Iterable interface, the AbstractList class implements this as follows:

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

If you use standard Java classes, you can expect this to be done this way.

Otherwise, you can do a simple test by calling iterator() form an object, and then run first and after this second, if they depend, the second should not give any result. But this is unlikely.

+1
source

You can 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); } } } 
+1
source

With parallel collections, you can have multiple iterators in different threads, even if they are inserted and deleted.

0
source

Source: https://habr.com/ru/post/1343815/


All Articles