Change to HashMap hash function in Java 8

后端 未结 3 1802
青春惊慌失措
青春惊慌失措 2021-02-04 11:41

In java 8 java.util.Hashmap I noticed a change from:

static int hash(int h) {
    h ^= (h >>> 20) ^ (h >>> 12);
    return h ^ (h >>>          


        
相关标签:
3条回答
  • 2021-02-04 12:16

    When I ran hash implementation diffences I see time difference in nano seconds as below (not great but can have some effect when the size is huge ~1million+)-

    7473 ns – java 7

    3981 ns– java 8

    If we are talking about well formed keys and hashmap of big size (~million), this might have some impact and this is because of simplified logic.

    0 讨论(0)
  • 2021-02-04 12:16

    As Java documentation says that idea is to handle the situation where old implementation of Linked list performs O(n) instead of O(1). This happens when same hash code is generated for large set of data being inserted in HashMap.

    This is not normal scenario though. To handle a situation that once the number of items in a hash bucket grows beyond a certain threshold, that bucket will switch from using a linked list of entries to a binary tree. In the case of high hash collisions, this will improve search performance from O(n) to O(log n) which is much better and solves the problem of performance.

    0 讨论(0)
  • 2021-02-04 12:21

    As you noted: there is a significant performance improvement in HashMap in Java 8 as described in JEP-180. Basically, if a hash chain goes over a certain size, the HashMap will (where possible) replace it with a balanced binary tree. This makes the "worst case" behaviour of various operations O(log N) instead of O(N).

    This doesn't directly explain the change to hash. However, I would hypothesize that the optimization in JEP-180 means that the performance hit due to a poorly distributed hash function is less important, and that the cost-benefit analysis for the hash method changes; i.e. the more complex version is less beneficial on average. (Bear in bind that when the key type's hashcode method generates high quality codes, then gymnastics in the complex version of the hash method are a waste of time.)

    But this is only a theory. The real rationale for the hash change is most likely Oracle confidential.

    0 讨论(0)
提交回复
热议问题