private static int i = 0; @Override public boolean equals(Object obj) { return true; } @Override public int hashCode() { return i++; }
public static void main(String[] args) { Person person1 = new Person(); Person person2 = new Person(); System.out.println(person1.equals(person2)); //输出true System.out.println(person1.hashCode()==person2.hashCode()); //输出false } }
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
public class Test { public static void main(String[] args) { A a1 = new A(1) ; A a2 = new A(2) ;
System.out.println(a1.equals(a2)) ; System.out.println(a1.hashCode()+" : "+a2.hashCode()) ; } } class A{ private int i ; public A(int i){ this.i = i ; }
值相同,内存地址不一定相同 如: public class T { public static void main(String[] args) { int a=0; int b=0; T t = new T(); T t1 = new T(); a=t.hashCode(); b=t1.hashCode(); if(a==b){ System.out.println("true"); } }}
Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes. (API) API是这么规定的。
先判断是否为null,如果 为null,直接返回false;再次判断是否是同一个类的对象。如果不是同一个类的对象,就没必要比的了(比如:人和一台电脑来比根本就没有意义),直接返回false;再次比较hashcode值是否相等。相等才返回true,否则返回false;从这个逻辑可以看出,两个对象的equals相等,一定有相同的hashcode值。反过来说就对了。有相同的hashcode值,这两个对象不一定相等....
原则是要求有一样的hashCode,但是不重写也是可以的啊,不重写就不保证相等了
对象equals了,我们就认为他们是相等的了,而一般不去判断它的hashcode,但是在对象做索引的时候就会去重写hashcode方法
其他的时候就没必要去重写了。
public class Person {
private static int i = 0;
@Override
public boolean equals(Object obj) {
return true;
} @Override
public int hashCode() {
return i++;
}
public static void main(String[] args) {
Person person1 = new Person();
Person person2 = new Person();
System.out.println(person1.equals(person2)); //输出true
System.out.println(person1.hashCode()==person2.hashCode()); //输出false
}
}
equals比较的是内容吧!hashcode指的的是分配内存空间的一个编码
http://blog.csdn.net/myjava_024/archive/2008/11/26/3376555.aspx
hashCode 的常规协定是: 在 Java 应用程序执行期间,在对同一对象多次调用 hashCode 方法时,必须一致地返回相同的整数,前提是将对象进行 equals 比较时所用的信息没有被修改。从某一应用程序的一次执行到同一应用程序的另一次执行,该整数无需保持一致。
如果根据 equals(Object) 方法,两个对象是相等的,那么对这两个对象中的每个对象调用 hashCode 方法都必须生成相同的整数结果。
如果根据 equals(java.lang.Object) 方法,两个对象不相等,那么对这两个对象中的任一对象上调用 hashCode 方法不要求一定生成不同的整数结果。但是,程序员应该意识到,为不相等的对象生成不同整数结果可以提高哈希表的性能。
两个对象值相同(x.equals(y) == true),但却可有不同的hash code
是正确的 是可以有不同的hashcode的
但是一般不应该这样做 这样不利于提高哈希表的性能 程序员应该意识到,为相等的对象生成相等的hashcode ,为不相等的对象生成不同整数结果可以提高哈希表的性能。 hashcode返回该对象的哈希码值。支持此方法是为了提高哈希表(例如 java.util.Hashtable 提供的哈希表)的性能当equal方法被重写时,通常有必要重写 hashCode 方法,以维护 hashCode 方法的常规协定,该协定声明相等对象必须具有相等的哈希码
equals()不相等的两个对象,却并不能证明他们的hashcode()不相等
String b = new String("123");
String a="123";
System.out.println(a.equals(b)); //输出true
System.out.println(a.hashCode()==b.hashCode()); //输出false
System.out.println(a==b);//true
}
public static void main(String[] args) {
String b = new String("123");
String a="123";
System.out.println(a.equals(b)); //输出true
System.out.println(a.hashCode()==b.hashCode()); //输出true
System.out.println(a==b);//false
}
String y = "test";
String a = "te";
String b = a ;
System.out.println(x.equals(y));
System.out.println(x.hashCode() == y.hashCode());
System.out.println(a.equals(b));
System.out.println(a.hashCode() == b.hashCode());我这里全部都是true
public static void main(String[] args) {
A a1 = new A(1) ;
A a2 = new A(2) ;
System.out.println(a1.equals(a2)) ;
System.out.println(a1.hashCode()+" : "+a2.hashCode()) ;
}
}
class A{
private int i ;
public A(int i){
this.i = i ;
}
public boolean equals(Object a){
return true;
}
public int hashCode(){
return this.i ;
}
}
两个对象有相同的hashcode,不一定相等
只是在文档中建议:
如果重新了equals方法的话,最好重新hashcode方法。
如果这样说的话,那就相等
但是那不是必须的
两个对象相等 他们的hashcode不一定相等
实际当中有可能发生这种情况的
先判断是否为null,如果 为null,直接返回false;再次判断是否是同一个类的对象。如果不是同一个类的对象,就没必要比的了,直接返回false;再次比较hashcode值是否相等。相等才返回true,否则返回false;从这个逻辑可以看出,两个对象的equals相等,一定有相同的hashcode值。反过来说就对了。有相同的hashcode值,这两个对象不一定相等。仅供楼主参考。
==操作比较的是两个变量的值是否相等,对于引用型变量表示的是两个变量在堆中存储的地址是否相同,即栈中的内容是否相同。</div>
equals操作表示的两个变量是否是对同一个对象的引用,即堆中的内容是否相同。
==比较的是2个对象的地址,而equals比较的是2个对象的内容。
显然,当equals为true时,==不一定为true;</div>
我简单说两句:
两个对象相等不一定具有相同的hashcode,
但是hashcode相同,却是相同的对象。
所以,人家的笔试题说的是正确的。
如果两个对象equals相等,其hashcode必相等。
如果两个对象equals不等,其hashcode可能相等,可能不等。
如果两个对象hashcode相等,其equals可能相等,可能不等。
如果两个对象hashcode不等,其equals必不等。
楼主的前提是equals结果是相等的,所以hash code一定是相等的。我认为你的分析比较到位。
楼主可以去看看《Effective Java》,里面有讲这个
在一般的应用中你不需要了解hashcode的用法,但当你用到hashmap,hashset等集合类时要注意下hashcode。 你想通过一个object的key来拿hashmap的value,hashmap的工作方法是,通过你传入的object的hashcode在内存中找地址,当找到这个地址后再通过equals方法来比较这个地址中的内容是否和你原来放进去的一样,一样就取出value。 所以这里要匹配2部分,hashcode和equals
但假如说你new一个object作为key去拿value是永远得不到结果的,因为每次new一个object,这个object的hashcode是永远不同的,所以我们要重写hashcode,你可以令你的hashcode是object中的一个恒量,这样永远可以通过你的object的hashcode来找到key的地址,然后你要重写你的equals方法,使内存中的内容也相等
如:
public class T { public static void main(String[] args) {
int a=0;
int b=0;
T t = new T();
T t1 = new T();
a=t.hashCode();
b=t1.hashCode();
if(a==b){
System.out.println("true");
}
}}
群号:100613346 欢迎你加入大家庭