Speedup Your Collections with Improved Hashing Function from Java7

Java7 has introduced an improved hash function for Map and Map derived implementations (since 7u6). It is applicable for keys of type String.

Following collections can make use of this new hash function.
The new hash function improves the performance when there are large number of collisions

It can be enabled by setting the System property jdk.map.althashing.threshold.
If jdk.map.althashing.threshold=k ,
This means that java will use alternative hashing function for maps with capacity greater than k entries.

Usage Example -
java -Djdk.map.althashing.threshold=512 MyAppMain

Why it is disabled by default and caveat before using -
  • This feature is disabled by default to ensure backward compatibility. 
  • The iteration order of keys, values and entries will change after using the new Hash function. According to Java team, many applications have dependencies on iteration order and therefore they have decided to disable this by default. 
  • If your application logic has dependencies on iteration order of Map/Set, then you should remove those dependencies before using new Hash function. 
  • Ideally applications should not make any assumptions regarding iteration order and therefore it is a good time to refactor your code and utilize the new Hashing function. 

References -
Reddit discussion for this post -
 

Comments

Popular posts from this blog

Prefer ThreadLocalRandom over Random

Shortest Distance Graph Algorithms - How do they differ?

Java - One liner to Configure Logging