HashMap Performance when overriding hashcode method

后端 未结 5 975
悲&欢浪女
悲&欢浪女 2021-02-06 10:34

In a HashMap, if I put custom objects as a key.

What would happen if I override hashCode() method and implement it to pass value as \'1

相关标签:
5条回答
  • 2021-02-06 11:07

    Adding Math.random() doesn't affect much performance hit but it is a bad idea to construct hashcode values through random() function. Instead you can use some of the good hashing function to minimize the collision and which are much faster also. For reference you can check out some of the links http://www.partow.net/programming/hashfunctions/

    0 讨论(0)
  • 2021-02-06 11:10

    Always returning 1 in hashCode() will degrade the performance of HashMap. Every object defaults to the same bucket, and the hash tables become linked lists. According to Effective Java, item 9, you get quadratic time instead of linear.

    Returning a random value will violate the provision that equal objects have equal hashCodes, you won't be able to retrieve the stored objects.

    0 讨论(0)
  • 2021-02-06 11:24

    If you are referring to the asymptotic time complexity then:

    since HashMap uses hashCode to calculate which bucket to use in the hashtable if you return 1 from hashCode you effectively make your HashMap's performance be like an (unsorted) LinkedList's performance.

    Returning random values will simply blow your HashMap up since equal objects will no longer have equal hashCodes.

    Excerpt from Wikipedia:

    +----------------------+----------+------------+----------+--------------+
    |                      |  Insert  |   Delete   |  Search  | Space Usage  |
    +----------------------+----------+------------+----------+--------------+
    | Unsorted linked list | O(1)*    | O(1)*      | O(n)     | O(n)         |
    | Hash table           | O(1)     | O(1)       | O(1)     | O(n)         |
    +----------------------+----------+------------+----------+--------------+
    

    So to sum it up you lose:

    • Time complexity when searching your HashMap (from O(1) to O(n))
    • lookup in your HashMap (it won't work anymore)
    0 讨论(0)
  • 2021-02-06 11:25

    If you always returned 1 (or any other constant value for all objects to be inserted), then the HashMap would internally degrade to be a "linked list". This means that insertion, deletion and querying would no longer have a complexity of O(1), but of O(n), and impose a potentially severe performance penalty.

    If you returned random values, then the HashMap would simply become inconsistent. It could happen that the "same" key appeared twice (although according to the specification, each key may only appear once). It may also happen that you will not find the value for a certain key, although you previously inserted it (with a different hashCode).

    The exact behavior will then also depend on the implementation of the equals method, but these are the main effects that such an implementation would have.

    0 讨论(0)
  • 2021-02-06 11:25

    Returning a fixed value in hashcode() will definitely make your hashtable run slower. All values will be assigned to the same bin, therefore lookup operations will take linear time (instead of the average constant time with a decent hash function).

    Returning a random value will break the hashmap contract completely. Values will be assigned to random bins and looked up in random bins, so nothing guarantees you'll find values stored previously.

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