If every object added to a java.util.HashSet implements Object.equals() and Object.hashCode() in a deterministic fashion, is the iteration order over the HashSet guaranteed to be identical for every identical set of elements added, irrespective of the order in which they were added?
Bonus question: what if the insertion order is identical as well?
(Assuming Sun JDK6 with same HashSet initialization.)
Edit: My original question was not clear. It is not about the general contract of HashSet, but what Sun's implementation of HashSet in JDK6 offers as guarantees concerning determinism. Is it inherently non-deterministic? What influences the order used by its Iterator?
HashSet
for iteration order?", or, more specifically, "Given a specific set of elements, does Java guarantee a deterministic iteration order forHashSet
?" For the OP, this is important because you cannot guarantee that your code will always be run by a specific Java JDK. – Fragmentary