dagblog.
← Archive

Watch out for minor Java updates


Java 7u6 (Update 6) and Java 8 are due to change the hashing algorithm for string keys in all standard java.util.Map implementations (HashMap, Hashtable, LinkedHashMap, WeakHashMap and ConcurrentHashMap). The designers expect the change to cut the risk of collisions and the resulting slowdowns. You can read more in the announcement. What’s interesting about this patch is the following note.

The iteration order of keys, values and entries for hash-based maps where the new algorithm has been invoked will vary for each HashMap instance. While the Java SE Map documentation makes no promises that iteration order of items returned from Maps will be consistent, developers should check if their applications have incorrectly created a dependency on the iteration order of Map entries, keys or values.

Even though the Map contract says this explicitly, I’d bet there’s plenty of code that depends, even indirectly, on the order in which keys are returned. To me this is a backward-incompatible change, even if it only takes effect under a particular condition. That may make it worse, since ordinary tests won’t catch it. It definitely shouldn’t have gone into a Java 7 update. I’ve been burned more than once by blindly trusting that Java updates are compatible and risk-free and giving the go-ahead because what could possibly go wrong? So consider this a warning. Watch out for this update. The bugs in your code may surface only under very specific conditions, which makes them especially nasty.

Medium version

Related articles

Related CZ Podcast episodes