Tuesday, May 26, 2020

Java hashCode()

  • Objects that are equal must have the same hash code within a running process
  • Whenever you implement equals, you MUST also implement hashCode
  • Whenever two different objects have the same hash code, we call this a collision.
  • A collision is nothing critical, it just means that there is more than one object in a single
    bucket, so a HashMap lookup has to look again to find the right object. A lot of collisions will degrade the performance of a system, but they won’t lead to incorrect results.
  • It is good to generate same hash code in different execution of programs but you should not relay on this. String and Integer are generating same hash code always will be same.But while most of the hashCode implementations provide stable values, you must not rely on it.here are Java libraries that actually return different hashCode values in different processes and this tends to confuse people. Google’s Protocol Buffers is an example.
  • Do not use hashCode in distributed applications
  • You may know that cryptographic hash codes such as SHA1 are sometimes used to identify objects (Git does this, for example). Is this also unsafe? No. SHA1 uses 160-bit keys, which makes collisions virtually impossible. Even with a gigantic number of objects, the odds of a collision in this space are far below the odds of a meteor crashing the computer that runs your program. This article has a great overview of collision probabilities.
  • A cryptographic hash such as MD5 or SHA-1 would be ok for many cases, but might be a bit heavyweight if you’re dealing with a really high-throughput service.

Sources


No comments:

Post a Comment

Thank you for your comment!