“`” 不对,两个对象的 hashCode() 相同,equals() 不一定 true。代码示例:

<pre><code>String str1 = ""精彩"";String str2 = ""笔记"";System. out. println(String. format(""str1:%d | str2:%d"", str1. hashCode(),str2. hashCode()));System. out. println(str1. equals(str2));</code></pre>

执行的结果:str1:1179395 | str2:1179395false

代码解读:很显然“精彩”和“笔记”的 hashCode() 相同,然而 equals() 则为 false,因为在散列表中,hashCode() 相等即两个键值对的哈希值相等,然而哈希值相等,并不一定能得出键值对相等。

<pre><code> "“`

Was this helpful?

0 / 0

发表回复 0

Your email address will not be published.