java - 同一个集合 : do they have to return elements in the same order? 的两个 java.util.Iterators

标签 java api collections iterator contract

这更像是一个理论问题。如果我有一个未排序的任意集合 c 并且我通过调用 c.iterator() 获得了两个 java.util.Iterator两次,两个迭代器是否必须以相同的顺序返回 c 的元素?

我的意思是,在实践中他们可能总是会这样做,但他们是根据契约(Contract)被迫这样做的吗?

谢谢, 简

最佳答案

不,他们不是。

"There are no guarantees concerning the order in which the elements are returned (unless this collection is an instance of some class that provides a guarantee)."

参见 Collection#iterator api contract .

这包括从一个迭代器到下一个迭代器(因为它没有说明任何要求)。

还要考虑在获取这两个迭代器之间底层集合中可能发生了一些变化!添加或删除的内容。

关于java - 同一个集合 : do they have to return elements in the same order? 的两个 java.util.Iterators,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10734953/

相关文章:

java - Android:JSON 解析错误没有出现

java - 将搜索模型转移到 Dao 的模式

java - 使用带有 JavaScript 和/或 Lua 的 Java 脚本 API

java - 如何迭代(并更改)作为另一个集合的值的集合

java - 无法创建 sessionFactory 对象.java.lang.NullPointerException

Java泛型方法

javascript - Node.js - 从 JSON 对象引用解析图像

json - 使用 RSpec 测试 jbuilder View 时如何分配变量

java - 如何在java中对List中的map进行排序

scala - 在 Scala 中编写带有偏移量的通用 takeWhile