是否有任何理由不能使用hashCode方法测试Java字符串的相等性?所以基本上,而不是....
"hello".equals("hello")
Run Code Online (Sandbox Code Playgroud)
你可以用......
"hello".hashCode() == "hello".hashCode()
Run Code Online (Sandbox Code Playgroud)
这很有用,因为一旦字符串计算了它的哈希码,那么比较一个字符串就像比较一个int一样有效,因为字符串缓存了哈希码,而且很可能字符串在字符串池中,如果你设计它办法.