概述
如何判断两个对象相等,这个问题实际上可以看做是如何对equals方法和hashcode方法的理解。
从以下几个点来理解equals和hashCode方法:
1、equals的作用及与==的区别。
2、hashcode的作用及与equals的关系。
1、equals的作用及与==的区别
equals通常用来比较两个对象的内容是否相等,==用来比较两个对象的地址是否相等。
equals方法默认(不被重写的话)等同于“==”
Object类中的equals方法定义为判断两个对象的地址是否相等(可以理解成是否是同一个对象),地址相等则认为是对象相等。这也就意味着,我们新建的所有类如果没有复写equals方法,那么判断两个对象是否相等时就等同于“==”,也就是两个对象的地址是否相等。
Object类中equals的方法实现如下:
1 public Boolean equals(Object obj) { 2 return (this == obj); 3 }
但在我们的实际开发中,通常会认为两个对象的内容相等时,则两个对象相等,equals返回true。对象内容不同,则返回false。
所以可以总结为两种情况
1、类未复写equals方法,则使用equals方法比较两个对象时,相当于==比较,即两个对象的地址是否相等。地址相等,返回true,地址不相等,返回false。
2、类复写equals方法,比较两个对象时,则走复写之后的判断方式。通常,我们会将equals复写成:当两个对象内容相同时,则equals返回true,内容不同时,返回false。
举个例子:
1 public class EqualTest { 2 public static void main(String[] args) { 3 Person p1 = new Person(10,"张三"); 4 Person p2 = new Person(10,"张三"); 5 System.out.println(p1.equals(p2)); 6 } 7 } 8 class Person{ 9 int age; 10 String name; 11 public Person(int age, String name) { 12 super(); 13 this.age = age; 14 this.name = name; 15 } 16 public int getAge() { 17 return age; 18 } 19 public void setAge(int age) { 20 this.age = age; 21 } 22 public String getName() { 23 return name; 24 } 25 public void setName(String name) { 26 this.name = name; 27 } 28 }
Person未复写equals方法,则默认使用了Object中的equals,即为两个对象(p1和p2)的内存地址判断,p1和p2很明显内存地址不同,所以输出结果很明显为false。
如果我们复写equals方法呢?我们认为名字和年龄一样的就是同一个人,那么p1和p2都表示10岁的张三,这两个对象应该是相等的。复写的equals方法如下:
1 @Override 2 public Boolean equals( Object obj ) 3 { 4 if ( this == obj ) //判断是不是同一地址 5 return(true); 6 if ( obj == null ) //判断是否为空 7 return(false); 8 if ( getClass() != obj.getClass()) //判断是不是同一类型 9 return(false); 10 Person other = (Person) obj; //是的话,强转对象,并且属性判断是否为空,然后判断是否相等。全部一致,说明两个对象为空 11 if ( age != other.age ) 12 return(false); 13 if ( name == null ) 14 { 15 if ( other.name != null ) 16 return(false); 17 } else if ( !name.equals( other.name ) ) 18 return(false); 19 return(true); 20 }
同样的,执行上述用例,得到的结果是true。
BTW:如果equals方法返回true,那么==是否也是true?
不一定是true。equals返回true有两种可能,一种是两个对象地址相同,一种是两个对象内容相同。当内容相同时,地址可能不同,所以==比较的结果可能为false。
我们把main方法加上对==的判断,如下:
1 public static void main( String[] args ) 2 { 3 Person p1 = new Person( 10, "张三" ); 4 Person p2 = new Person( 10, "张三" ); 5 System.out.println( p1.equals( p2 ) ); 6 System.out.println( p1 == p2 ); 7 }
输出结果很明显 p1==p2的结果是false。
补充Java中对Equals的要求:
1. 对称性:如果x.equals(y)返回是"true",那么y.equals(x)也应该返回是"true"。
2. 反射性:x.equals(x)必须返回是"true"。
3. 类推性:如果x.equals(y)返回是"true",而且y.equals(z)返回是"true",那么z.equals(x)也应该返回是"true"。
4. 一致性:如果x.equals(y)返回是"true",只要x和y内容一直不变,不管你重复x.equals(y)多少次,返回都是"true"。
5. 非空性,x.equals(null),永远返回是"false";x.equals(和x不同类型的对象)永远返回是"false"。
2、hashCode的作用及与equals的关系。
hashCode的作用是用来获取哈希码,也可以称作散列码。实际返回值为一个int型数据。用于确定对象在哈希表中的位置。
Object中有hashcode方法,也就意味着所有的类都有hashCode方法。
但是,hashcode只有在创建某个类的散列表的时候才有用,需要根据hashcode值确认对象在散列表中的位置,但在其他情况下没用。
java中本质上是散列表的类常见的有HashMap,HashSet,HashTable
所以,如果一个对象一定不会在散列表中使用,那么是没有必要复写hashCode方法的。但一般情况下我们还是会复写hashCode方法,因为谁能保证这个对象不会出现再hashMap等中呢?
举个例子:
两个对象equals相等的时候,hashcode并不一定相等。
1 public class EqualTest { 2 public static void main( String[] args ) 3 { 4 Person p1 = new Person( 10, "张三" ); 5 Person p2 = new Person( 10, "张三" ); 6 System.out.println( 7 "p1.equals(p2)=" + p1.equals( p2 ) + ", p1.hashcode=" + p1.hashCode() + ", p2.hashcode=" + p2.hashCode() ); 8 } 9 } 10 class Person { 11 int age; 12 String name; 13 public Person( int age, String name ) 14 { 15 super(); 16 this.age = age; 17 this.name = name; 18 } 19 20 21 public int getAge() 22 { 23 return(age); 24 } 25 26 27 public void setAge( int age ) 28 { 29 this.age = age; 30 } 31 32 33 public String getName() 34 { 35 return(name); 36 } 37 38 39 public void setName( String name ) 40 { 41 this.name = name; 42 } 43 44 45 @Override 46 public boolean equals( Object obj ) 47 { 48 if ( this == obj ) 49 return(true); 50 if ( obj == null ) 51 return(false); 52 if ( getClass() != obj.getClass() ) 53 return(false); 54 Person other = (Person) obj; 55 if ( age != other.age ) 56 return(false); 57 if ( name == null ) 58 { 59 if ( other.name != null ) 60 return(false); 61 } else if ( !name.equals( other.name ) ) 62 return(false); 63 return(true); 64 } 65 }
Person没有复写hashCode方法,使用Object默认的hashCode实现,输出结果如下:
p1.equals(p2)=true, p1.hashcode=246688959, p2.hashcode=1457895203
从结果可以看出,equals虽然相同,但是p1和p2的hashcode并不相同。
如果Person用于散列表的类中呢,这里用HashSet来做测试。
1 public class EqualTest { 2 public static void main( String[] args ) 3 { 4 Person p1 = new Person( 10, "张三" ); 5 Person p2 = new Person( 10, "张三" ); 6 System.out.println( 7 "p1.equals(p2)=" + p1.equals( p2 ) + ", p1.hashcode=" + p1.hashCode() + ", p2.hashcode=" + p2.hashCode() ); 8 HashSet<Person> set = new HashSet<Person>(); 9 set.add( p1 ); 10 set.add( p2 ); 11 System.out.println( set ); 12 } 13 } 14 class Person { 15 int age; 16 String name; 17 public Person( int age, String name ) 18 { 19 super(); 20 this.age = age; 21 this.name = name; 22 } 23 24 25 public int getAge() 26 { 27 return(age); 28 } 29 30 31 public void setAge( int age ) 32 { 33 this.age = age; 34 } 35 36 37 public String getName() 38 { 39 return(name); 40 } 41 42 43 public void setName( String name ) 44 { 45 this.name = name; 46 } 47 48 49 @Override 50 public boolean equals( Object obj ) 51 { 52 if ( this == obj ) 53 return(true); 54 if ( obj == null ) 55 return(false); 56 if ( getClass() != obj.getClass() ) 57 return(false); 58 Person other = (Person) obj; 59 if ( age != other.age ) 60 return(false); 61 if ( name == null ) 62 { 63 if ( other.name != null ) 64 return(false); 65 } else if ( !name.equals( other.name ) ) 66 return(false); 67 return(true); 68 } 69 70 71 @Override 72 public String toString() 73 { 74 return("Person [age=" + age + ", name=" + name + "]"); 75 } 76 }
输出结果
p1.equals(p2)=true, p1.hashcode=246688959, p2.hashcode=1457895203
[Person [age=10, name=张三], Person [age=10, name=张三]]
p1和p2的equals相同,我们认为是两个对象相等,但是这两个对象竟然同时出现再hashSet中,hashSet中是不会出现两个相同的元素的。
那问题在哪里?
hashset在添加一个元素的时候,会做如下判断:
1、如果添加元素的hashcode相等并且 对象equals为true或对象== 时,则认为是同一个元素,不添加到新元素中。
2、如果不符合上述条件,则认为是一个新元素,添加到set中。
所以,虽然p1和p2equals比较时相等,但是hashcode并不一样,所以在往set中添加的时候认为是两个不同的元素,所以才会出现了p1和p2同时在set中的情况。
我们改进下,复写一下hashcode方法,如下:
1 class Person { 2 int age; 3 String name; 4 public Person( int age, String name ) 5 { 6 super(); 7 this.age = age; 8 this.name = name; 9 } 10 11 12 public int getAge() 13 { 14 return(age); 15 } 16 17 18 public void setAge( int age ) 19 { 20 this.age = age; 21 } 22 23 24 public String getName() 25 { 26 return(name); 27 } 28 29 30 public void setName( String name ) 31 { 32 this.name = name; 33 } 34 35 36 @Override 37 public int hashCode() 38 { 39 final int prime = 31; 40 int result = 1; 41 result = prime * result + age; 42 result = prime * result + ( (name == null) ? 0 : name.hashCode() ); 43 return(result); 44 } 45 46 47 @Override 48 public boolean equals( Object obj ) 49 { 50 if ( this == obj ) 51 return(true); 52 if ( obj == null ) 53 return(false); 54 if ( getClass() != obj.getClass() ) 55 return(false); 56 Person other = (Person) obj; 57 if ( age != other.age ) 58 return(false); 59 if ( name == null ) 60 { 61 if ( other.name != null ) 62 return(false); 63 } else if ( !name.equals( other.name ) ) 64 return(false); 65 return(true); 66 } 67 68 69 @Override 70 public String toString() 71 { 72 return("Person [age=" + age + ", name=" + name + "]"); 73 } 74 }
重新执行结果:
p1.equals(p2)=true, p1.hashcode=776160, p2.hashcode=776160
[Person [age=10, name=张三]]
于是看到set中仅有一个Person值了。
补充几点:
1、新建一个类,尤其是业务相关的对象类的时候,最好复写equals方法。
2、复写equals方法时,同时记着要复写hashCode方法,谁能保证说这个对象一定不会出现在hashMap中呢?如果你用的是eclipse的自动代码生成,你会发现eclipse中复写equals和hashCode是在一起的。
引申出几个经常在面试中问到的问题:
1、两个对象,如果a.equals(b)==true,那么a和b是否相等?
相等,但地址不一定相等。
2、两个对象,如果hashcode一样,那么两个对象是否相等?
不一定相等,判断两个对象是否相等,需要判断equals是否为true。(hashcode算法可能相同,但是他们内容是不同的)
原文:https://blog.csdn.net/u013063153/article/details/78808923
转载于:https://www.cnblogs.com/denglaomo/articles/10763583.html
最后
以上就是缥缈毛衣为你收集整理的35号: Java中如何判断两个对象是否相等(equals and hashcode)的全部内容,希望文章能够帮你解决35号: Java中如何判断两个对象是否相等(equals and hashcode)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复