What is a sensible prime for hashcode calculation?

后端 未结 6 789
故里飘歌
故里飘歌 2020-11-29 16:27

Eclipse 3.5 has a very nice feature to generate Java hashCode() functions. It would generate for example (slightly shortened:)

class HashTest {
    int i;
           


        
6条回答
  •  青春惊慌失措
    2020-11-29 16:40

    Actually, if you take a prime so large that it comes close to INT_MAX, you have the same problem because of modulo arithmetic. If you expect to hash mostly strings of length 2, perhaps a prime near the square root of INT_MAX would be best, if the strings you hash are longer it doesn't matter so much and collisions are unavoidable anyway...

提交回复
热议问题