只重写equals()但不重写hashCode会有什么后果?

转载自https://www.cnblogs.com/guanmu/p/4894430.html


首先,说下equals和hashCode的关系。JDK API中关于Object类的equals和hashCode方法中说过,总结起来就是两句话:equals相等的两个对象的hashCode也一定相等,但hashCode相等的两个对象不一定equals相等。

 

 hashCode类似于一个位置值(不叫地址值,是想把每个对象所在的位置做地址值),HashSet、HashMap等集合类中常会用到。

只重写equals()但不重写hashCode会有什么后果?_第1张图片

上图中假设是对象在内存中的模型,则7~c就是位置值,即hashCode。而71—74就是对象地址值。所以x,y和z的hashCode是一样的,但是x、y和z的equals不一定相等equals相等 只跟自己类定义的equals方法有关。

假设x是A类的对象,y是B类对象,z是C类对象,A、B和C的equals()都实现为始终返回true,则程序会认为x、y和z是相等的;若都实现为返回false,则x跟自己都不相等。

即equals()是否相等,只依据自定义的equals()方法的判断结果。

  当我们重写equals方法时,是有要求的(具体见JDK)。如果只重写了equals,不重写hashCode会有什么影响呢?

  假如该类不会放在HashSet等散列表相关的集合类中,不一定会有影响,如下代码:

Model类:

package com.guanmu.test;


/**
 * 

* 类描述: *

* * 所属插件:com.guanmu.test * * @author guanmu 2015-10-20 * */ public class Model { private String name; private String age; private String otherName; /** * @param name * @param age * @param otherName */ public Model(String name, String age, String otherName) { super(); this.name = name; this.age = age; this.otherName = otherName; } /** * @return the name */ public String getName() { return name; } /** * @param name * the name to set */ public void setName(String name) { this.name = name; } /** * @return the age */ public String getAge() { return age; } /** * @param age * the age to set */ public void setAge(String age) { this.age = age; } /** * @return the otherName */ public String getOtherName() { return otherName; } /** * @param otherName * the otherName to set */ public void setOtherName(String otherName) { this.otherName = otherName; } @Override public int hashCode() { int a = 7; int b = 11; // a和b为不相等的int型常量 int r = a; r = r*b + name.hashCode(); r = r*b + age.hashCode(); // return r; return super.hashCode(); } /* 注意这个重写的equals()只用了name与age这两个属性!!! */ @Override public boolean equals(Object obj) { if (!(obj instanceof Model)) { return false; } Model other = (Model) obj; if (name.equals(other.getName()) && age.equals(other.getAge())) { return true; } return false; } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "Model [name=" + name + ", age=" + age + ", otherName=" + otherName + "]"; } }

测试类:

package com.guanmu.test;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 

* 类描述: *

* * 所属插件:com.guanmu.test * @author guanmu2015-10-20 * */ public class EqualsTest { public static void main(String[] args) { test1(); test2(); } public static void test1() { System.out.println("####:test1"); List list = new ArrayList(); Model a = new Model("a", "20", "test"); Model b = new Model("a","20","abcdef"); Model c = new Model("a","20",""); list.add(a); System.out.println("a-hashCode:" + a.hashCode()); System.out.println(list); if (!list.contains(b)) { list.add(b); System.out.println("b-hc:" + b.hashCode()); System.out.println(list); } } public static void test2() { System.out.println("####:test2"); Set set = new HashSet(); Model a = new Model("a", "20", "test"); Model b = new Model("a","20","abcdef"); Model c = new Model("a","20",""); set.add(a); System.out.println("a-hashCode:" + a.hashCode()); System.out.println(set); if (!set.contains(b)) { set.add(b); System.out.println("b-hc:" + b.hashCode()); System.out.println(set); } } }

结果打印如下:

####:test1
a-hashCode:18923308
[Model [name=a, age=20, otherName=test]]
####:test2
a-hashCode:15136722
[Model [name=a, age=20, otherName=test]]
b-hc:26752749
[Model [name=a, age=20, otherName=test], Model [name=a, age=20, otherName=abcdef]]

  1. list中能判断出包含了等价的b(因为a和b的name和age属性相同,所以我们自定义的equals()方法就认为a与b等价)
  2. 但set中认为a和b不相等,set中没有包含b。
  • 发现test1()中,ArrayList只根据equals()来判断两个对象是否相等,而不管hashCode是否不相等。
  • 而test2()中,HashSet判断流程则不一样,①先判断两个对象的hashCode方法是否一样;②如果不一样,立即认为两个对象equals不相等,并不调用equals方法;③当hashCode相等时,再根据equals方法判断两个对象是否相等。

  总结,所以当我们所写的类可能用于存放在Hash相关的集合类中时,在重写equals时,需要重写hashCode,不然会出现与预期不符的结果。从网上搜索了下如何重写hashCode方法,以下据说是《Think in Java》中提到的方法。

@Override
    public int hashCode() {
        
        int a = 7;
        int b = 11;
        // a和b为不相等的int型常量
        int r = a;
        r = r*b + name.hashCode();
        r = r*b + age.hashCode();
        
        return r;
    }

其中a和b可以为任意不相等常量。

 

你可能感兴趣的:(Java)