Java 中的集合可以有多个迭代器吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5315441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can a collection have multiple iterators in Java?
提问by cp.
Is it possible to have multiple iterators in a single collection and have each keep track independently? This is assuming no deletes or inserts after the iterators were assigned.
是否可以在一个集合中拥有多个迭代器并让每个迭代器独立跟踪?这是假设在分配迭代器后没有删除或插入。
回答by ColinD
Yes.
是的。
Sometimes it's really annoying that answers have to be 30 characters.
有时答案必须是 30 个字符真的很烦人。
回答by Bozho
Yes, it is possible. That's one reason they are iterators, and not simply methods of the collection.
对的,这是可能的。这就是它们是迭代器的原因之一,而不仅仅是集合的方法。
For example List
iterators (defined in AbstractList
) hold an int
to the current index (for the iterator). If you create multiple iterators and call next()
a different number of times, each of them will have its int cursor
with a different value.
例如,List
迭代器(在 中定义AbstractList
)保存int
当前索引(对于迭代器)。如果您创建多个迭代器并调用next()
不同的次数,它们中的每一个都将具有int cursor
不同的值。
回答by Damian Leszczyński - Vash
Yes and no. That depend of the implementation of the interface Iterable<T>
.
是和否。这取决于接口的实现Iterable<T>
。
Usually it should return new instance of a class that implement Iterable interface, the class AbstractList implements this like that:
通常它应该返回实现 Iterable 接口的类的新实例,类 AbstractList 实现如下:
public Iterator<E> iterator() {
return new Itr(); //Where Itr is an internal private class that implement Itrable<T>
}
If you are using standard Java classes You may expect that this is done this way.
如果您使用的是标准 Java 类,您可能期望这是通过这种方式完成的。
Otherwise You can do a simple test by calling iterator()
form the object and then run over first and after that second one, if they are depend the second should not produce any result. But this is very unlikely possible.
否则,您可以通过调用iterator()
form 对象来做一个简单的测试,然后首先运行,然后在第二个之后运行,如果它们是依赖的,则第二个不应产生任何结果。但这不太可能。
回答by tooleyc
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);
}
}
}
回答by Peter Lawrey
With the concurrent collections you can have multiple iterators in different threads even if there inserts and deletes.
使用并发集合,即使存在插入和删除操作,您也可以在不同线程中拥有多个迭代器。