JAVA中重写equals()方法为什么要重写hashcode()方法?
2013-01-10 17:01
761 查看
object对象中的 public boolean equals(Object obj),对于任何非空引用值 x 和 y,当且仅当 x 和 y 引用同一个对象时,此方法才返回 true;
注意:当此方法被重写时,通常有必要重写 hashCode 方法,以维护 hashCode 方法的常规协定,该协定声明相等对象必须具有相等的哈希码。如下:
(1)当obj1.equals(obj2)为true时,obj1.hashCode() == obj2.hashCode()必须为true
(2)当obj1.hashCode() == obj2.hashCode()为false时,obj1.equals(obj2)必须为false
如果不重写equals,那么比较的将是对象的引用是否指向同一块内存地址,重写之后目的是为了比较两个对象的value值是否相等。特别指出利用equals比较八大包装对象
(如int,float等)和String类(因为该类已重写了equals和hashcode方法)对象时,默认比较的是值,在比较其它自定义对象时都是比较的引用地址
hashcode是用于散列数据的快速存取,如利用HashSet/HashMap/Hashtable类来存储数据时,都是根据存储对象的hashcode值来进行判断是否相同的。
这样如果我们对一个对象重写了euqals,意思是只要对象的成员变量值都相等那么euqals就等于true,但不重写hashcode,那么我们再new一个新的对象,
当原对象.equals(新对象)等于true时,两者的hashcode却是不一样的,由此将产生了理解的不一致,如在存储散列集合时(如Set类),将会存储了两个值一样的对象,
导致混淆,因此,就也需要重写hashcode()
举例说明:
[java] view plaincopyprint?
import java.util.*;
public class HelloWorld {
public static void main(String[] args) {
/*
Collection c = new HashSet();
c.add("hello");
c.add(new Name("f1","l1"));
c.add(new Integer(100));
c.remove("hello");
c.remove(new Integer(100));
System.out.println(c.remove(new Name("f1","l1")));
*/
Name n1 = new Name("01");
Name n2 = new Name("01");
Collection c = new HashSet();
c.add(n1);
System.out.println("------------");
c.add(n2);
System.out.println("------------");
System.out.println(n1.equals(n2));
System.out.println("------------");
System.out.println(n1.hashCode());
System.out.println(n2.hashCode());
System.out.println(c);
}
}
class Name {
private String id;
public Name(String id) {
this.id = id;
}
public String toString(){
return this.id;
}
public boolean equals(Object obj) {
if (obj instanceof Name) {
Name name = (Name) obj;
System.out.println("equal"+ name.id);
return (id.equals(name.id));
}
return super.equals(obj);
}
public int hashCode() {
Name name = (Name) this;
System.out.println("Hash" + name.id);
return id.hashCode();
}
}
就这个程序进行分析,在第一次添加时,调用了hashcode()方法,将hashcode存入对象中,第二次也一样,然后对hashcode进行比较。hashcode也只用于HashSet/HashMap/Hashtable类存储数据,所以会用于比较,需要重写
总结,自定义类要重写equals方法来进行等值比较,自定义类要重写compareTo方法来进行不同对象大小的比较,重写hashcode方法为了将数据存入HashSet/HashMap/Hashtable类时进行比较.
import java.util.Date;
class Dog{
private String name;
private Date birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
public class Cat {
/**Cat类中含有name和birthday两私有成员变量,且重写了equals方法和hashCode方法
*
* @param name 和 birthday
*
*/
private String name;
private Date birthday;
public Cat(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
/*
* 重写equals必须注意:
* 1 自反性:对于任意的引用值x,x.equals(x)一定为true
* 2 对称性:对于任意的引用值x 和 y,当x.equals(y)返回true,y.equals(x)也一定返回true
* 3 传递性:对于任意的引用值x、y和z,如果x.equals(y)返回true,并且y.equals(z)也返回true,那么x.equals(z)也一定返 回 true
* 4 一致性:对于任意的引用值x 和 y,如果用于equals比较的对象信息没有被修改,
* 多次调用x.equals(y)要么一致地返回true,要么一致地返回false
* 5 非空性:对于任意的非空引用值x,x.equals(null)一定返回false
*
* 请注意:
* 重写equals方法后最好重写hashCode方法,否则两个等价对象可能得到不同的hashCode,这在集合框架中使用可能产生严重后果
*/
/*
* 1.重写equals方法修饰符必须是public,因为是重写的Object的方法.
* 2.参数类型必须是Object.
*/
public boolean equals(Object other){ //重写equals方法,后面最好重写hashCode方法
if(this == other) //先检查是否其自反性,后比较other是否为空。这样效率高
return true;
if(other == null)
return false;
if( !(other instanceof Cat))
return false;
final Cat cat = (Cat)other;
if( !getName().equals(cat.getName()))
return false;
if( !getBirthday().equals(cat.getBirthday()))
return false;
return true;
}
public int hashCode(){ //hashCode主要是用来提高hash系统的查询效率。当hashCode中不进行任何操作时,可以直接让其返回 一常数,或者不进行重写。
int result = getName().hashCode();
result = 29 * result +getBirthday().hashCode();
return result;
//return 0;
}
public static void main(String[] args) {
Date dayA = new Date(4000000);
Cat a = new Cat();
a.setName("a");
a.setBirthday(dayA);
Date dayB = new Date(1000000);
Cat b = new Cat();
b.setName("a");
b.setBirthday(dayB);
Date dayC = dayA;
Cat c = new Cat();
c.setName("a");
c.setBirthday(dayC);
Date dayE = dayA;
Cat e = new Cat();
e.setName(a.getName());
e.setBirthday(a.getBirthday());
Date dayD = dayC;
Dog d = new Dog();
d.setName("a");
d.setBirthday(dayD);
System.out.println(a.equals(b)); //调用自己类中所定义的equals方法
System.out.println(a.equals(a));
System.out.println(a.equals(c));
System.out.println(d.equals(a));
System.out.println(a.equals(d)); //验证重写equals的对称性
System.out.println(a.equals(e));
System.out.println(e.equals(c)); //验证重写equals的传递性
System.out.println(d.getName().equals(a.getName())); //调用Object类中equals方法
System.out.println(d.getBirthday().equals(b.getBirthday()));
System.out.println("比较hanshCode的值");
/*
* * 比较hashCode方法中返回的值
* 如果equals返回为true,则hashCode一定返回true。
* 如果equals返回为false,hashCode返回值不一定不相同。
* 如果hashCode返回值不同,则equals返回值一定为false。
* 如果hashCode返回值不同,则equals返回值不一定为false。
*/
System.out.println(a.hashCode());
System.out.println(b.hashCode());
System.out.println(a.hashCode()== b.hashCode()); //如果equals返回false,则各hashCode不一定返回不同值
System.out.println(a.hashCode() == c.hashCode());
}
}
注意:当此方法被重写时,通常有必要重写 hashCode 方法,以维护 hashCode 方法的常规协定,该协定声明相等对象必须具有相等的哈希码。如下:
(1)当obj1.equals(obj2)为true时,obj1.hashCode() == obj2.hashCode()必须为true
(2)当obj1.hashCode() == obj2.hashCode()为false时,obj1.equals(obj2)必须为false
如果不重写equals,那么比较的将是对象的引用是否指向同一块内存地址,重写之后目的是为了比较两个对象的value值是否相等。特别指出利用equals比较八大包装对象
(如int,float等)和String类(因为该类已重写了equals和hashcode方法)对象时,默认比较的是值,在比较其它自定义对象时都是比较的引用地址
hashcode是用于散列数据的快速存取,如利用HashSet/HashMap/Hashtable类来存储数据时,都是根据存储对象的hashcode值来进行判断是否相同的。
这样如果我们对一个对象重写了euqals,意思是只要对象的成员变量值都相等那么euqals就等于true,但不重写hashcode,那么我们再new一个新的对象,
当原对象.equals(新对象)等于true时,两者的hashcode却是不一样的,由此将产生了理解的不一致,如在存储散列集合时(如Set类),将会存储了两个值一样的对象,
导致混淆,因此,就也需要重写hashcode()
举例说明:
[java] view plaincopyprint?
import java.util.*;
public class HelloWorld {
public static void main(String[] args) {
/*
Collection c = new HashSet();
c.add("hello");
c.add(new Name("f1","l1"));
c.add(new Integer(100));
c.remove("hello");
c.remove(new Integer(100));
System.out.println(c.remove(new Name("f1","l1")));
*/
Name n1 = new Name("01");
Name n2 = new Name("01");
Collection c = new HashSet();
c.add(n1);
System.out.println("------------");
c.add(n2);
System.out.println("------------");
System.out.println(n1.equals(n2));
System.out.println("------------");
System.out.println(n1.hashCode());
System.out.println(n2.hashCode());
System.out.println(c);
}
}
class Name {
private String id;
public Name(String id) {
this.id = id;
}
public String toString(){
return this.id;
}
public boolean equals(Object obj) {
if (obj instanceof Name) {
Name name = (Name) obj;
System.out.println("equal"+ name.id);
return (id.equals(name.id));
}
return super.equals(obj);
}
public int hashCode() {
Name name = (Name) this;
System.out.println("Hash" + name.id);
return id.hashCode();
}
}
import java.util.*; public class HelloWorld { public static void main(String[] args) { /* Collection c = new HashSet(); c.add("hello"); c.add(new Name("f1","l1")); c.add(new Integer(100)); c.remove("hello"); c.remove(new Integer(100)); System.out.println(c.remove(new Name("f1","l1"))); */ Name n1 = new Name("01"); Name n2 = new Name("01"); Collection c = new HashSet(); c.add(n1); System.out.println("------------"); c.add(n2); System.out.println("------------"); System.out.println(n1.equals(n2)); System.out.println("------------"); System.out.println(n1.hashCode()); System.out.println(n2.hashCode()); System.out.println(c); } } class Name { private String id; public Name(String id) { this.id = id; } public String toString(){ return this.id; } public boolean equals(Object obj) { if (obj instanceof Name) { Name name = (Name) obj; System.out.println("equal"+ name.id); return (id.equals(name.id)); } return super.equals(obj); } public int hashCode() { Name name = (Name) this; System.out.println("Hash" + name.id); return id.hashCode(); } }
就这个程序进行分析,在第一次添加时,调用了hashcode()方法,将hashcode存入对象中,第二次也一样,然后对hashcode进行比较。hashcode也只用于HashSet/HashMap/Hashtable类存储数据,所以会用于比较,需要重写
总结,自定义类要重写equals方法来进行等值比较,自定义类要重写compareTo方法来进行不同对象大小的比较,重写hashcode方法为了将数据存入HashSet/HashMap/Hashtable类时进行比较.
java中重写equals方法
2010-01-15 16:04 3643人阅读 评论(0) 收藏 举报import java.util.Date;
class Dog{
private String name;
private Date birthday;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
public class Cat {
/**Cat类中含有name和birthday两私有成员变量,且重写了equals方法和hashCode方法
*
* @param name 和 birthday
*
*/
private String name;
private Date birthday;
public Cat(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
/*
* 重写equals必须注意:
* 1 自反性:对于任意的引用值x,x.equals(x)一定为true
* 2 对称性:对于任意的引用值x 和 y,当x.equals(y)返回true,y.equals(x)也一定返回true
* 3 传递性:对于任意的引用值x、y和z,如果x.equals(y)返回true,并且y.equals(z)也返回true,那么x.equals(z)也一定返 回 true
* 4 一致性:对于任意的引用值x 和 y,如果用于equals比较的对象信息没有被修改,
* 多次调用x.equals(y)要么一致地返回true,要么一致地返回false
* 5 非空性:对于任意的非空引用值x,x.equals(null)一定返回false
*
* 请注意:
* 重写equals方法后最好重写hashCode方法,否则两个等价对象可能得到不同的hashCode,这在集合框架中使用可能产生严重后果
*/
/*
* 1.重写equals方法修饰符必须是public,因为是重写的Object的方法.
* 2.参数类型必须是Object.
*/
public boolean equals(Object other){ //重写equals方法,后面最好重写hashCode方法
if(this == other) //先检查是否其自反性,后比较other是否为空。这样效率高
return true;
if(other == null)
return false;
if( !(other instanceof Cat))
return false;
final Cat cat = (Cat)other;
if( !getName().equals(cat.getName()))
return false;
if( !getBirthday().equals(cat.getBirthday()))
return false;
return true;
}
public int hashCode(){ //hashCode主要是用来提高hash系统的查询效率。当hashCode中不进行任何操作时,可以直接让其返回 一常数,或者不进行重写。
int result = getName().hashCode();
result = 29 * result +getBirthday().hashCode();
return result;
//return 0;
}
public static void main(String[] args) {
Date dayA = new Date(4000000);
Cat a = new Cat();
a.setName("a");
a.setBirthday(dayA);
Date dayB = new Date(1000000);
Cat b = new Cat();
b.setName("a");
b.setBirthday(dayB);
Date dayC = dayA;
Cat c = new Cat();
c.setName("a");
c.setBirthday(dayC);
Date dayE = dayA;
Cat e = new Cat();
e.setName(a.getName());
e.setBirthday(a.getBirthday());
Date dayD = dayC;
Dog d = new Dog();
d.setName("a");
d.setBirthday(dayD);
System.out.println(a.equals(b)); //调用自己类中所定义的equals方法
System.out.println(a.equals(a));
System.out.println(a.equals(c));
System.out.println(d.equals(a));
System.out.println(a.equals(d)); //验证重写equals的对称性
System.out.println(a.equals(e));
System.out.println(e.equals(c)); //验证重写equals的传递性
System.out.println(d.getName().equals(a.getName())); //调用Object类中equals方法
System.out.println(d.getBirthday().equals(b.getBirthday()));
System.out.println("比较hanshCode的值");
/*
* * 比较hashCode方法中返回的值
* 如果equals返回为true,则hashCode一定返回true。
* 如果equals返回为false,hashCode返回值不一定不相同。
* 如果hashCode返回值不同,则equals返回值一定为false。
* 如果hashCode返回值不同,则equals返回值不一定为false。
*/
System.out.println(a.hashCode());
System.out.println(b.hashCode());
System.out.println(a.hashCode()== b.hashCode()); //如果equals返回false,则各hashCode不一定返回不同值
System.out.println(a.hashCode() == c.hashCode());
}
}
相关文章推荐
- JAVA中重写equals()方法为什么要重写hashcode()方法说明
- JAVA中重写equals()方法为什么要重写hashcode()方法?
- JAVA中重写equals()方法为什么要重写hashcode()方法?
- java中为什么重写equals时必须重写hashCode方法?
- 【Java基础之重写equals、hashCode和compareTo方法】什么时候需要重写重写equals方法?为什么重写equals方法,一定要重写HashCode方法?
- Java重写equals方法时为什么要重写hashcode方法
- java中重写equals()方法的时候为什么要重写hashCode()方法?
- JAVA中重写equals()方法为什么要重写hashcode()方法?
- JAVA中重写equals()方法为什么要重写hashcode()方法说明
- 【Java基础】JAVA中重写equals()方法为什么要重写hashcode()方法?
- JAVA中重写equals()方法为什么要重写hashcode()方法?
- JAVA中重写equals()方法为什么要重写hashcode()方法说明
- Java中equals和==的区别?为什么重写equals方法后,一定要重写hashCode方法?
- JAVA中重写equals()方法为什么要重写hashcode()方法?
- java为什么要重写hashCode和equals方法
- JAVA中重写equals()方法为什么要重写hashcode()方法?
- JAVA中重写equals()方法为什么要重写hashcode()方法?
- JAVA中重写equals()方法为什么要重写hashcode()方法?
- Java 重写equals方法为什么还必须重写hashCode方法
- JAVA中重写equals()方法为什么要重写hashcode()方法?