问题描述
这更是一个理论问题。如果我有一个乱收 C
未下令,我获得两个 java.util.Iterator的
通过调用 c.iterator()
两次,一举两得迭代器必须返回在同一顺序的 C
元素?
This is more of a theoretical question. If I have an arbitrary collection c
that isn't ordered and I obtain two java.util.Iterator
s by calling c.iterator()
twice, do both iterators have to return c
's elements in the same order?
我的意思是,在实践中,他们可能永远会,但他们不得不通过合同这样做呢?
I mean, in practice they probably always will, but are they forced to do so by contract?
谢谢,
扬
推荐答案
没有事实并非如此。
有关于该元素的顺序不能保证
返回(除非该集合是某个类的一个实例
提供了担保)。
查看Collection#iterator API契约。
这包括从一个迭代到下(因为它没有说关于要求任何东西)。
That includes from one iterator to the next (as it doesn't say anything about requiring that).
另外还要考虑的东西可以在底层集合在得到这两个迭代器之间变了!一些添加或删除。
Also consider that something could have changed in the underlying collection between getting those two iterators! Something added or removed.
这篇关于二java.util.Iterators相同的集合:他们要以相同的顺序返回元素呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!