views:

288

answers:

9

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?

+1  A: 

No, this is not guaranteed.

First, different JVM may implement the HashSet algorithm differently (as long as it complies with the HashSet specification) so you will get different results on different JVMs.

Second, the algorithm may rely on non-deterministic factors when it builds the different buckets (part of the hash-table algorithm).

Itay
I'm using the same JVM. I specifically mention that all hash codes are deterministic (i.e. Object.hashCode() is always overrided in a meaningful and deterministic way).
eljenso
+3  A: 

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.

ewernli
My original question was not clear. Sorry for that. Your answer is correct though in a general sense.
eljenso
+6  A: 

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.

Péter Török
A: 

I am sure that the Java developers want you to assume the answer is "no". In particular, for hash tables, why would they make it slower for everyone else who doesn't need this property to guarantee that objects whose hashes clash (identical hashCode % size) are observed in the same order regardless of the order in which they were put in?

Pascal Cuoq
+4  A: 

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)

Michael Borgwardt
Great answer. I edited in a small bonus question: what if the insertion order remains identical as well? In other words: is there anything inherently non-deterministic in the implementation of the "standard" java.util.HashMap?
eljenso
@eljenso: I'm pretty sure there isn't - but I don't see how to prove that conclusively.
Michael Borgwardt
A: 

Such assumption cannot be made. The javadoc says that:

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 closest you can get is to use a LinkedHashSet, which maintains the insertion order.

ryanprayogo
A: 

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.

ahe