What is a sensible prime for hashcode calculation?

后端 未结 6 782
故里飘歌
故里飘歌 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条回答
  •  萌比男神i
    2020-11-29 16:35

    I just want to point out that hashcode has nothing to do with prime. In JDK implementation

    for (int i = 0; i < value.length; i++) {
                    h = 31 * h + val[i];
                }
    

    I found if you replace 31 with 27, the result are very similar.

提交回复
热议问题