`
xiangdefei
  • 浏览: 57507 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

浅淡equals()和hashcode()

 
阅读更多
以前对Object类的equals()方法和hashcode()方法很模糊,只知道用equals()来判断两个对象是否相等,hashcode()是来计算散列值的,重写equals()方法时,总是要重写hashcode(),却不知道为什么要这样,昨天在家研究了一天,终于弄懂了一点点,所以写下来加深印象,不懂的同学也可以看看。

   在网上查了很多资料,但是都写得不明不白的,压根写那些资料的人自己都不明白,还出来误导人!靠你大爷的。于是我查了Effective+java,think in java 两本书,但是里面有些讲得很难懂(大师们写得太深奥了!!) ,加之又是看英文翻译过来的中文版,(-.-|||....   好后悔以前没有认真滴学英语) 所以看起来很费力。于是我就把里面的每一句话都理解然后再写代码做测试。好了,不啰嗦了。切入正题!

       想必大家都知道,equals()方法和hashcode()方法都是从Object类中继承过来的。而String ,Integer、Double。Float等值类的这些封装类都重写了Object中的equals()方法,让它们不再比较引用,而是比较对象中实际包含的值,即比较的是内容。
    而Object的equals()方法比较的是地址值(也是比较引用是否相同)。

equals()方法在Object类中是这样定义的:

public boolean equals(Object obj) {
return (this == obj);
}



String,Integer等封装类重写Obejct类的equals()方法,大家自己看吧 。


      首先请问一下,判断java两个对象是否相等的规则是什么?

     比较两个对象是否相等,首先比较两个对象的hashcode值是否相等,如果hashcode值不相等的话,那么我们认为这两个对象肯定不相等,如果hashcode值相等,那么我们再用equals(Ojbect)比较一下这两个对象是否相等,如果不相等,我们认为这两个对象不相等,如果相等,这两个对象肯定相等,也就是说如果两个对象根据equals(Object)方法是相等的,那么调用两个对象中任何一个对象的hashcode方法必须产生相同的值 ,反之,如果两个对象根据equals(Object)方法是不相等的,那么调用两个对象中任何一个对象的hashcode方法不要求必须产生不相同的值(既两个对象equals()不相等,它们也有可能有相等的hashcode值)  ,也或者可以这么说:两个对象hashcode()不相等,那么equals()肯定不相等,两个对象hashcode相等,equals()有可能相等,也有可能不相等。OK?明白了不?(观众:靠你大爷的,咋这么绕?都被你说糊涂了)。那上面的话有什么根据呢?我来解释一下

Object类是这样定义hashcode()方法的:

Public native int hashcode()  ;


说明hashcode()是一个本地方法,返回的是对象的地址值,而Object类的equals()方法是比较两个对象地址值的,如果equals()相等,那么说明两个对象具有相等的地址值,所以hashcode()值也相等。在String,Integer等类中,equals()方法是比较两个对象的内容,当两个对象内容相等时,Hashcode()方法根据String等类的重写(这个前面已经分析了)代码的分析,也可知道hashcode()返回结果也会相等。

一般来说,如果你要把一个类的对象放入容器中,那么通常要为其重写equals()方法,让他们比较地址值而不是内容值。特别地,如果要把你的类的对象放入散列表容器中(Hashtable、HashMap、HashSet、LinkedHashMap),那么还要重写hashCode()方法;要放到有序容器中,还要重写compareTo()方法。那么下面解释一下为什么要重写equals()和hashcode()?(这也是本文的重点)(观众:靠你大爷的,那你前面说的那些都是废话么?答:NO NO NO,前面说的都是为这个做铺垫,如果你看不懂前面说的那些,这个....)

下面我们用Set容器做例子:

我们都知道Set不能存储相同的元素,元素的位置也是不确定的(无序的)。那么Set又是怎么去判断是否有重复对象的呢?前面有解释(看看看…明白了上面说的不是废话了吧),不废话,写段代码测试一下,这样更明白点:

public static void main(String args[]){

String str1=new String("xiangdefei");

       String str2=new String("xiangdefei");

       Set set=new HashSet();

       set.add(str1);

       set.add(str2);

       System.out.println(str1==str2);

       System.out.println(str1.hashCode()==str2.hashCode());

       System.out.println("str1.hashcode:"+str1.hashCode());

       System.out.println("str2.hashcode:"+str2.hashCode());

       System.out.println(str1.equals(str2));    

       Iterator ite=set.iterator();

       while(ite.hasNext()){

           System.out.println(ite.next());

       }

}

打印结果:

False

true

str1.hashcode:523358144

str2.hashcode:523358144

true

xiangdefei



果在我们预料之中,在while循环里只打印了一个:xiangdefei,

因为String类重写了equals()和hashcode()方法,我们也看到了这两个String类的对象的hashcode值相等,都是523358144,所以集合认定这两个对象相等,所以不重复添加。

但是我们看看下面这个例子:

这是一个普通的类Student(现在没有为这个类重写equals()和hashcode()方法,只重写了toString()方法(方便等下打印结果)):

public class Student {

    private String name;

    private int age;

    

 

    public String getName() {

       return name;

    }

    public void setName(String name) {

       this.name = name;

    }

    public int getAge() {

       return age;

    }

    public void setAge(int age) {

       this.age = age;

    }

public String toString(){

       return this.name+":"+this.age;

    }

}

Main方法:

public static void main(String args[]){

Student student1=new Student("xiangdefei",1);

       Student student2=new Student("xiangdefei",1);

       Set set=new HashSet();

       set.add(student1);

       set.add(student2);

       System.out.println(student1==student2);

       System.out.println(student1.hashCode()==student2.hashCode());

       System.out.println(student1.hashCode());

       System.out.println(student2.hashCode());

       System.out.println(student1.equals(student2));

       Iterator ite=set.iterator();

       while(ite.hasNext()){

           System.out.println(ite.next());

       }

}

打印结果为:

false

false

33311724

14452073

false

xiangdefei:1

xiangdefei:1




是不是郁闷了?为什么Set集合添加了重复元素?

仔细看看两个对象的hashcode值,不相等。那么equals()比较也不相等。所以容器认为它们是不相等的元素,进行了重复添加。它们为什么没有产生相等的hashcode值呢,前面的例子的两个对象不都产生了相等的hashcode值了吗?前面解释了,String类重写了Object类的equals()和hashcode(),而这个Student类没有重写equals()和hashcode()方法。所以集合在比较这两个对象用的是Object类的hashcode(),各位还记得hashcode是比较什么的吗?(观众:你丫的哪来的这么多问题?自己回答!!!),是的,前面提过hashcode()比较的是对象的地址值(对象的引用),而我们使用new创建的两个对象,两个对象的地址值当然不一样,所以Set把它们当作两个不同的对象,那怎么办呢?

对了,就是在Student类中重写Object类的equals()和 hashcode()方法

public class Student {

    private String name;

    private int age;

    

 

    public String getName() {

       return name;

    }

    public void setName(String name) {

       this.name = name;

    }

    public int getAge() {

       return age;

    }

    public void setAge(int age) {

       this.age = age;

    }

public String toString(){

       return this.name+":"+this.age;

    }

 

@Override

    public int hashCode() {

       final int prime = 31;

       int result = 1;

       result = prime * result + age;

       result = prime * result + ((name == null) ? 0 : name.hashCode());

       return result;

    }

    @Override

    public boolean equals(Object obj) {

       if (this == obj)

           return true;

       if (obj == null)

           return false;

       if (getClass() != obj.getClass())

           return false;

       Student other = (Student) obj;

       if (age != other.age)

           return false;

       if (name == null) {

           if (other.name != null)

              return false;

       } else if (!name.equals(other.name))

           return false;

       return true;

    }

}

修改Student类,如果再运行一下前面的main方法则是打印这样的结果:

false

true

523359136

523359136

true

xiangdefei:1





产生了相同的hashcode值,所以Set集合认为这两个对象是相等的,只添加了一个元素。

关于如何在类中重写equals()和hashcode()方法,今天就不说了,我自己也是半懂不懂的。等弄懂了,下次再写吧。好累!(观众:鼓掌!!!!)

2
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics