Iteration order of HashSet
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?
Absolutely not.
The insertion order directly influences the iteration order whenever you have a bucket collision:
When two elements end up in the same bucket, the first one that was inserted will also be the first one returned during iteration, at least if the implementation of collision handling and iteration is straightforward (and the one in Sun's java.util.HashMap
is)
There is no "official" guarantee for anything like this. I would say it is most probably true for instances of the same HashSet implementation, initialized the same way. But I have seen cases for the iteration order being different between Java 5 and 6, for example.
Also, it may be different for instances of the same HashSet implementation, initialized with different size, due to rehashing. I.e. if you have 100 elements and two sets, one initialized with a size greater than 100, the other with a much smaller size, the second one will get reallocated and its elements rehashed several times while filling up. This may result in elements mapped to the same bucket being added (and thus iterated over) in different order.
In Java4 and later, you have LinkedHashSet
which guarantees that the iteration order will be the order in which its elements were inserted.
Wanted to confirm / upvote earlier comments. In short, Do Not Rely on HashSet iteration in consistent order. This can and will introduce bugs in your system.
We just found and fixed a bug where the iteration order was inconsistent in HashSet even with:
- Identical insertion order.
- Objects of a class with a valid equals() and hashCode() method.
And fixed it by using LinkedHashSet.
Thanks to the earlier posters :)
As per the javadoc:
This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. [...] The iterators returned by this class's iterator method are fail-fast: if the set is modified at any time after the iterator is created
And the method iterator
:
Returns an iterator over the elements in this set. The elements are returned in no particular order.
So I don't think you can make such an assumption.
Never ever make assumptions about the iteration order of anything you put into a HashSet because its contract explicitly says that you can't count on it in any way. Use LinkedHashSet if you want to maintain insertion order or TreeSet if you want to maintain a natural sorting order.