A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 李征 中级黑马   /  2013-5-12 22:52  /  1142 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 李征 于 2013-5-12 23:40 编辑

请问在覆盖hashCode方法的时候,毕老师为什么要在age后面*34呢?始终不理解,有没有高手帮小弟解答一下,感激不尽!
代码如下:
import java.util.*;
class MapDemo
{
        public static void main(String[] Args)
        {
                HashMap<Student,String> hm= new HashMap<Student,String>();
               
                hm.put(new Student("lisi1",21),"beijing");
                hm.put(new Student("lisi1",21),"beng");
                hm.put(new Student("lisi2",22),"beijin");
                hm.put(new Student("lisi3",23),"beiji");
                //第一种取出方式 keySet
                Set<Student> keySet = hm.keySet();

                Iterator<Student> it = keySet.iterator();

                while(it.hasNext())
                {
                        Student stu = it.next();
                        String addr = hm.get(stu);
                        System.out.println(stu+".."+addr);
                }
                //第二种取出方式 entrySet
                Set<Map.Entry<Student,String>> entrySet = hm.entrySet();
                Iterator<Map.Entry<Student,String>>  iter = entrySet.iterator();

                while(iter.hasNext())
                {
                        Map.Entry<Student,String> me = iter.next();
                        Student stu = me.getKey();
                        String addr = me.getValue();
                        System.out.println(stu+"    "+addr);
                }
               
               
        }
}

class Student implements Comparable<Student>
{
        private String name;
        private int age;
        Student(String name,int age)
        {
                this.name = name;
                this.age = age;
        }        

        public int compareTo(Student s)
        {
                int num = new Integer(this.age).compareTo(new Integer(s.age));
                if(num==0)
                        return this.name.compareTo(s.name);
                return num;
        }
        
        public int hashCode()//覆盖2个方法
        {
                return name.hashCode()+age*34;//age 为什么要*34呢?
        }
        public boolean equals(Object obj)
        {
                if(!(obj instanceof Student))
                        throw new ClassCastException("类型不匹配");
                Student s = (Student)obj;

                return this.name.equals(s.name) && this.age==s.age;
        }


        public String getName()
        {
                return name;
        }
        public int getAge()
        {
                return age;
        }
        public String toString()
        {
                return "name:"+name+"age:"+age;
        }

        }

7 个回复

倒序浏览
占个沙发  帮你顶起来
回复 使用道具 举报
8047107 发表于 2013-5-12 22:52
占个沙发  帮你顶起来

谢谢哈~~
回复 使用道具 举报
呼呼。现实是,这个34可以被任何数字取代。
其实就是Student类的对象通过name 和age的hash值来决定两个对象是否相同
回复 使用道具 举报
乎¤_¤乎 发表于 2013-5-12 22:58
呼呼。现实是,这个34可以被任何数字取代。
其实就是Student类的对象通过name 和age的hash值来决定两个对象 ...

有点没懂,那这个数字有什么意义呢?
回复 使用道具 举报
就是为了最小概率让两个对象的hashcode值不同,提高效率。因为name的hashcode值不确定,例如:不使用age*34的话
new Student("lisi",21)假设对象的name.hashcode=30 age=21,则返回的是51,而这时候又有个对象new Student("zhangsan",31)他的name.hashcode=20  age=31,则返回的也是51,由于两个对象的hashcode值相同则会又去调用equals方法。
如果使用age*34的话两个对象的hashcode值就不会相同了,也就不必去调用equals方法去比较对象了。显然这样效率会高些。
另外,34不是硬性规定的,可以是别的其他数,当然你不用也是可以的。
回复 使用道具 举报
李征 中级黑马 2013-5-12 23:39:50
7#
赵崇友 发表于 2013-5-12 23:33
就是为了最小概率让两个对象的hashcode值不同,提高效率。因为name的hashcode值不确定,例如:不使用age*34 ...

这次看懂了,十分感谢你,太有帮助了
回复 使用道具 举报
李征 发表于 2013-5-12 23:39
这次看懂了,十分感谢你,太有帮助了

no感谢了,共同学习探讨。之前我还真没有认真思考过这个问题。哈哈
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马