Using a larger prime as a multiplier when overriding hashCode()

后端 未结 2 442
刺人心
刺人心 2020-12-15 23:40

I have been reading about hashcode functions for the past couple of hours and have accumulated a couple of questions regarding use of prime numbers as multipliers in custom

2条回答
  •  北海茫月
    2020-12-15 23:55

    • Overflow is not a problem. Hashes are constrained to a narrow value set anyway.

    • The first hash function you posted isn't very good. Doing return (prime * hash1) ^ hash2; ` instead would reduce the number of collisions in most cases.

    • Multiplying by a single word int is generally very fast, and the difference between multiplying by different numbers is negligible. Plus the execution time is dwarfed by everything else in the function anyay

    • Using different prime multipliers for each part may reduce the risk of collisions.

提交回复
热议问题