为什么哈希码值相同?
public static void main(String args[])
{
String s1="abc";
String s2=new String("abc");
System.out.println("Hashcode s1-:"+ s1.hashCode());
System.out.println("Hashcode s2-:"+ s2.hashCode());
if(s1==s2){
System.out.println("==true:");
}
}
输出
Hashcode s1-:96354
Hashcode s2-:96354
最佳答案
两个相等对象的哈希码应该相等。
在本例中,对象是字符串,并且它们被视为相等,因为它们包含相同的字符序列“abc”。
如果您想要基于对象标识而不是相等的哈希码,请使用 System.identityHashCode()
.
关于java - 哈希码值相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15473353/