Can a non-empty string have a hashcode of zero?

后端 未结 2 1716
离开以前
离开以前 2020-12-15 16:53

By \"non-empty\", I mean in this question a string which contains at least one non-zero character.

For reference, here\'s the hashCode implementation :<

2条回答
  •  抹茶落季
    2020-12-15 17:42

    just be care of that int h;. It may overflow, every string that satisfy h % 2^31 == 0 may lead to this.

    public class HelloWorld {
        public static void main(String []args) {
           System.out.println("\u0001!qbygvW".hashCode());
            System.out.println("9 $Ql(0".hashCode());
            System.out.println(" #t(}lrl".hashCode());
            System.out.println(" !!#jbw}a".hashCode());
            System.out.println(" !!#jbw|||".hashCode());
            System.out.println(" !!!!Se|aaJ".hashCode());
            System.out.println(" !!!!\"xurlls".hashCode());
        }
    }
    

    A lot of strings...

提交回复
热议问题