62,614
社区成员
发帖
与我相关
我的任务
分享
最简单的例子
public static void main(String[] args) {
Long x = -1L;
Long y = 0L;
System.out.println(x.hashCode());
System.out.println(y.hashCode());
}
package equals;
import java.util.HashSet;
/**
* @author faith.huan 2020-06-26 15:25
*/
public class HashCodeTest {
public static void main(String[] args) {
HashSet<Integer> set = new HashSet<>();
boolean flag = true;
while (flag) {
Object o = new Object();
if (set.contains(o.hashCode())) {
System.out.println("hashcode重复,setSize:" + set.size());
flag = false;
} else {
set.add(o.hashCode());
}
}
}
}
我的输出是 hashcode重复,setSize:105777
String s1= new String("s");
String s2= new String("s");
System.out.println(s1 == s2);
System.out.println(s1.hashCode() == s2.hashCode());