Java中equals方法和hashcode方法

class Point{
private final int x;
private final int y;
public Point(int x,int y){
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o){
if(!(o instanceof Point)) return false;

    Point p = (Point)o;
    return p.x==x&&p.y==y;
}

}

class ColorPoint extends Point{
private final Color color;

public ColorPoint(int x,int y,Color color){
    super(x,y);
    this.color = color;
}
@Override
public boolean equals(Object o){
    if(!(o instanceof  ColorPoint)) return false;

    return super.equals(o)&&((ColorPoint)o).color == color;
}

}
public class equalsTest {
public static void main(String[] args){
Point p = new Point(2,2);
ColorPoint cp = new ColorPoint(2,2,Color.BLACK);
System.out.println(p.equals(cp));//打印为true
System.out.println(cp.equals§);//打印为false
}
}
违背传递性,在上述代码中将ColorPoint类的equals方法改为如下:
class Point{
private final int x;
private final int y;
public Point(int x,int y){
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o){
if(!(o instanceof Point)) return false;

    Point p = (Point)o;
    return p.x==x&&p.y==y;
}

}

class ColorPoint extends Point{
private final Color color;

public ColorPoint(int x,int y,Color color){
    super(x,y);
    this.color = color;
}
@Override
public boolean equals(Object o){
    if(!(o instanceof  Point)) return false;
    if(!(o instanceof ColorPoint)) return o.equals(this);
    return super.equals(o)&&((ColorPoint)o).color == color;
}

}
public class equalsTest {
public static void main(String[] args){
ColorPoint cp = new ColorPoint(2,2,Color.BLACK);
Point p = new Point(2,2);
ColorPoint cp1 = new ColorPoint(2,2,Color.BLUE);
System.out.println(cp.equals§);//打印true
System.out.println(p.equals(cp1));//打印true
System.out.println(cp.equals(cp1));//打印false
}
}
对于一致性的话,不要使equals方法依赖不可靠资源,例如:java.net.URL的equals方法依赖于对URL中主机IP地址的比较。将一个主机名转变成IP地址可能需要访问网络,随着时间的推移,不确保会产生相同的结果。
编写一个完美的equals方法的建议:

检测this与otherObject是否引用同一个对象(先执行它是对equals方法的一个优化,用最不消耗性能的判断条件去过滤掉一部分):
if(this==otherObject) return true;
检测otherObject是否为null,如果为null,返回false:
if(otherObject == null) return false;
比较this与thisObject是否为同一个类。如果equals的语义在每个子类对象中有所改变(子类和超类使用不同的equals方法,很容易违背上述规约,而getClass就使得必须是同一个类的对象才返回true,可以避免违背规约),就使用getClass检测(但违背了里氏替换原则):
if(getClass() != otherObject.getClass()) return false;
如果所有子类的equals都不会修改的话,就使用instanceof:

if(!(otherObject instanceof ClassName)) return false;
将otherObject转换为相应的类类型变量:
ClassName other = (ClassName)otherObject
现在开始对所有需要比较的域进行比较了。使用== 比较基本类型域,使用 equals 比较对象域。如果所有的域都匹配, 就返回 true; 否 则 返 回 false。
return fieldl == other.field
&& Objects.equa1s(fie1d2, other.field2)
&&…
如果在子类中重新定义 equals, 就要在其中包含调用 super.equals(other)。
Tips:

两个对象引用字段x,y进行比较的时候,为了防止为null,可以调用Objects.equals方法,当两者都为null,返回true;当其中一个为空,则返回false;当两者都不为空时,则返回x.equals(y)的结果。
对于数组类型的字段(域), 可以使用静态的 Arrays.equals 方法检测相应的数组元素是否相等。
重写equals方法时,加上@override注解,否则,容易变成重载equals方法,而加上注解会提示是否为重写。如:
public boolean equals(Employee other)
{
return other != null
&& getClassO == other.getClass0
&& Objects.equals(name, other. name)
&& salary
&& Objects—.equals other(hireDay , sal ary , other.hireDa)
}
写完equals方法需要对其进行编写用例进行测试,看是否违反了其规约。
其参数应该为Object。
重写完equals方法一定需要重写hashcode方法
重写hashcode方法主要是为对象存放到散列表(hashtable,hashmap,hashset等)中做准备的,因为在散列表中涉及到大量的比较,而直接使用hashcode比较代价往往小于直接使用equals的代价,而hashcode的中也明确规定了两个对象根据equals方法比较相等,则二者hashcode必须相等,如果不重写就会违背这条,如:
class Point{
private final int x;
private final int y;
public Point(int x,int y){
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o){
if(!(o instanceof Point)) return false;

    Point p = (Point)o;
    return p.x==x&&p.y==y;
}

}
public class equalsTest {
public static void main(String[] args){
Point p1 = new Point(2,2);
Point p2 = new Point(2,2);

    System.out.println(p1.equals(p2));//打印为true
    System.out.println(p1.hashCode());//本地打印为460141958
    System.out.println(p2.hashCode());//本地打印为1163157884
}

}
二、 hashcode方法
在重写hashcode方法时,需要遵循的Object(hashcode方法来自与Obejct中)规范:

在应用程序执行期间,只要equals方法的比较操作所用到的信息没有被修改,那么对同一个对象调用多次,hashcode方法都必须始终如一的返回同一个整数。在同一个应用程序的多次执行过程中,每次执行所返回的整数可以不一致。
如果两个对象根据equals方法比较时相等的,那么调用这两个对象中任意一个对象的hashcode方法都必须产生同样的整数。
如果两个对象根据equals方法比较时不相等的,那么调用这两个对象中任意一个对象的hashcode方法,不一定产生不同的整数。但不同的对象,产生不同整数,有可能提高散列表的性能。
编写一个好散列表的解决办法:

把某个非零的常数值,如17,保存在一个名为result的int类型变量中。
对于对象中的每个关键域f(指equals方法中涉及的每个域),完成以下步骤:
如果字段为boolean类型,则计算(f?1:0)
如果字段为byte,char,short,或者int类型,则计算(int)f
如果字段为long类型,则计算(int)(f^(f>>>32))。
如果字段为float类型,则计算Float.floatToIntBits(f)。
如果字段为double类型,则计算Double.doubleToLongBits(f),然后为计算出的long类型值计算散列值。
如果字段为对象引用,并且该类的equals方法通过递归地调用equals的方式来比较这个域,则同样为这个域递归的调用hashcode。如果需要更复杂的比较,则为这个字段计算一个范式(解释范式),然后针对这个范式调用hashcode,如果这个字段为null,则返回0(或者其他某个常数,但通常是0)。
如果字段为一个数组,则把每一个元素当中单独的字段来处理,递归应用上面的规则。
将上面计算的所有散列码c合并到result中:
result = 31*result + c;
深圳网站建设www.sz886.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值