`
京华倦客
  • 浏览: 119894 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

hashMap的hashCode() 和equa()的使用 (转载)

阅读更多
import java.util.HashSet; 

import java.util.Iterator; 

import java.util.Set; 

  

import android.app.Activity; 

import android.os.Bundle; 

  

  

public class TestCollectionActivity extends Activity { 

    /** Called when the activity is first created. */

  

    @Override

    public void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.main); 

    

          

        Set<Person> hashSet = new HashSet<Person>(); 

        hashSet.add(new Person("张先生", 12121212)); 

        hashSet.add(new Person("张太太", 12121213)); 

        hashSet.add(new Person("张先生", 12121210)); 

        hashSet.add(new Person("网先生", 12121210)); 

        hashSet.add(new Person("网先生", 12121210)); 

        hashSet.add(new Person("张先生", 12121210)); 

        /** 最后结果输出为: 

         *       张先生 12121212 

            张太太 12121213 

            网先生 12121210 

            张先生 12121210 

              

            如果不添加hashCode()和equals(),所有person成员都将正常输出 

            添加hashCode和equals之后,若a.hashCode() = b.hashCode且equals返回为true,此时原先的成员将被替代, 

            导致了上面的结果输出 

        */

          

        Iterator<Person> it = hashSet.iterator(); 

        while(it.hasNext()) { 

            Person person = it.next(); 

            System.out.println(person.getName() + " " + person.getId_card()); 

        } 

          

    } 

      

    public class Person { 

        private String name; 

        private long id_card; 

        public Person(String name, long id_card) { 

            this.name = name; 

            this.id_card = id_card; 

        } 

          

        public long getId_card(){ 

            return id_card; 

        } 

          

        public String getName() { 

            return name; 

        } 

          

        public int hashCode() { 

            int result = 1; 

            result = (int)(id_card ^(id_card>>32)); 

            return result;
view sourceprint?
//          final int PRIME = 31; 

//          int result = 1; 

//          result = PRIME * result + (int)(id_card ^ (id_card >>32));//long型数据取(int)位 

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

//          return result;
view sourceprint?
        } 

          

        public boolean equals(Object obj) { 

            if (this == obj) 

                return true; 

            if (obj == null) 

                return false; 

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

                return false; 

            final Person other = (Person) obj; 

            if (id_card != other.id_card) 

                return false; 

            if (name == null) { 

                if (other.name != null) 

                    return false; 

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

                return false; 

            return true; 

        } 

    } 

}
 hashCode()和equal()由输出情况可得知:添加hashCode()和equals()之后,若a.hashCode() = b.hashCode且equals返回为true,此时原先的成员将被替代,导致了上面的结果输出。

   hashCode用于快速判断两个对象是否相等,如果相等,则根据equal的返回值确定是否覆盖,equal()返回true则覆盖。


下面是补充:

在java的集合中,判断两个对象是否相等的规则是:
1,判断两个对象的hashCode是否相等
    如果不相等,认为两个对象也不相等,完毕
    如果相等,转入2
2,判断两个对象用equals运算是否相等
    如果不相等,认为两个对象也不相等
    如果相等,认为两个对象相等
=====================================

1、 为什么要重载equal方法?

答案:因为Object的equal方法默认是两个对象的引用的比较,意思就是指向同一内存,地址则相等,否则不相等;如果你现在需要利用对象里面的值来判断是否相等,则重载equal方法。

2、 为什么重载hashCode方法?

答案:一般的地方不需要重载hashCode,只有当类需要放在HashTable、HashMap、HashSet等等hash结构的集合时才会重载hashCode,那么为什么要重载hashCode呢?就HashMap来说,好比HashMap就是一个大内存块,里面有很多小内存块,小内存块里面是一系列的对象,可以利用hashCode来查找小内存块hashCode%size(小内存块数量),所以当equal相等时,hashCode必须相等,而且如果是object对象,必须重载hashCode和equal方法。

3、 为什么equals()相等,hashCode就一定要相等,而hashCode相等,却不要求equals相等?

答案:1、因为是按照hashCode来访问小内存块,所以hashCode必须相等。

            2、HashMap获取一个对象是比较key的hashCode相等和equal为true。

之所以hashCode相等,却可以equal不等,就比如ObjectA和ObjectB他们都有属性name,那么hashCode都以name计算,所以hashCode一样,但是两个对象属于不同类型,所以equal为false。

4、 为什么需要hashCode?

1、 通过hashCode可以很快的查到小内存块。
2、 通过hashCode比较比equal方法快,当get时先比较hashCode,如果hashCode不同,直接返回false。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics