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

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

                  Java的重写equals但不重写hashCode方法的影响

  上图中假设是对象在内存中的模型,则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类

  1 package com.guanmu.test;
  2 
  3 
  4 /**
  5  * <p>
  6  * 类描述:
  7  * <p>
  8  * 
  9  * 所属插件:com.guanmu.test
 10  * 
 11  * @author guanmu 2015-10-20
 12  * 
 13  */
 14 public class Model {
 15 
 16     private String name;
 17     private String age;
 18     private String otherName;
 19 
 20     /**
 21      * @param name
 22      * @param age
 23      * @param otherName
 24      */
 25     public Model(String name, String age, String otherName) {
 26         super();
 27         this.name = name;
 28         this.age = age;
 29         this.otherName = otherName;
 30     }
 31 
 32     /**
 33      * @return the name
 34      */
 35     public String getName() {
 36         return name;
 37     }
 38 
 39     /**
 40      * @param name
 41      *            the name to set
 42      */
 43     public void setName(String name) {
 44         this.name = name;
 45     }
 46 
 47     /**
 48      * @return the age
 49      */
 50     public String getAge() {
 51         return age;
 52     }
 53 
 54     /**
 55      * @param age
 56      *            the age to set
 57      */
 58     public void setAge(String age) {
 59         this.age = age;
 60     }
 61 
 62     /**
 63      * @return the otherName
 64      */
 65     public String getOtherName() {
 66         return otherName;
 67     }
 68 
 69     /**
 70      * @param otherName
 71      *            the otherName to set
 72      */
 73     public void setOtherName(String otherName) {
 74         this.otherName = otherName;
 75     }
 76 
 77     @Override
 78     public int hashCode() {
 79         
 80         int a = 7;
 81         int b = 11;
 82         // a和b为不相等的int型常量
 83         int r = a;
 84         r = r*b + name.hashCode();
 85         r = r*b + age.hashCode();
 86         
 87 //        return r;
 88         return super.hashCode();
 89     }
 90     
 91     @Override
 92     public boolean equals(Object obj) {
 93         if (!(obj instanceof Model)) {
 94             return false;
 95         }
 96 
 97         Model other = (Model) obj;
 98         if (name.equals(other.getName()) && age.equals(other.getAge())) {
 99             return true;
100         }
101 
102         return false;
103     }
104 
105     /* (non-Javadoc)
106      * @see java.lang.Object#toString()
107      */
108     @Override
109     public String toString() {
110         return "Model [name=" + name + ", age=" + age + ", otherName=" + otherName + "]";
111     }
112     
113     
114 }
View Code

相关文章:

  • 2021-07-21
  • 2022-12-23
  • 2021-12-31
  • 2021-10-02
  • 2022-03-07
  • 2021-07-28
  • 2021-07-02
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-28
相关资源
相似解决方案