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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© pthuakai 中级黑马   /  2013-5-16 10:50  /  789 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 pthuakai 于 2013-5-16 14:03 编辑

package day16;
import java.util.*;


class Student11 implements Comparable<Student11>//实现一个比较方法。
{
        private String name;
        private int age;
        Student11(String name,int age)
        {
                this.name=name;
                this.age=age;
        }
        public int compareTo(Student11 s)//比较年龄大小。
        {
                //下面句子是干啥用的。关键字new Integer.age不是已经是int了,为啥不直接比较却要装箱后比较。
                int num=new Integer(this.age).compareTo(new Integer(s.age));
                if(num==0)
                        return this.name.compareTo(name);
                return num;
        }
        public int hashCode()//这个方法是返回此映射到hash码值。但是这又有什么作用呢?在该例子中,不是太明白。尤其是age*34是干什么用的。
        {
                return name.hashCode()+age*34;
        }
        public boolean equals(Object obj)//覆盖一个对象。是否是学生类对象。
        {
                if(!(obj instanceof Student11))
                        throw new ClassCastException("class not macth");
                Student11 s=(Student11)obj;
                return this.name.equals(s.name)&&this.age==age;
        }
        public String getName()
        {
                return name;
        }
        public int getAge()
        {
                return age;
        }
        public String toString()
        {
                return name+age;
        }
}
public class MapTest {

        /**
         * @param args
         */
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                HashMap<Student11, String> hm=new HashMap<Student11,String>();
                hm.put(new Student11("lisi1",21), "beijing");
                hm.put(new Student11("lisi2",22), "shanghai");
                hm.put(new Student11("lisi3",23), "tianjing");
                hm.put(new Student11("lisi3",23), "tianjing");
                hm.put(new Student11("lisi4",24), "wuhan");
               
                //第一种取出方式
                Set<Student11> keySet=hm.keySet();
                Iterator<Student11>it=keySet.iterator();
                while(it.hasNext())
                {
                        Student11 stu=it.next();
                        String add=hm.get(stu);
                        System.out.println(add+stu);
                }
                //第二中取出方式
                Set<Map.Entry<Student11, String>> entrySet=hm.entrySet();
                Iterator<Map.Entry<Student11, String>> iter=entrySet.iterator();
                while(iter.hasNext())
                {
                        Map.Entry<Student11, String> me=iter.next();
                        Student11 stu=me.getKey();
                        String addr=me.getValue();
                        System.out.println(stu+";;"+addr);
                }
               
               
               
                 
        }

}

评分

参与人数 1技术分 +1 收起 理由
袁梦希 + 1

查看全部评分

3 个回复

倒序浏览
本帖最后由 刘茂林 于 2013-5-16 10:59 编辑

http://bbs.itheima.com/thread-49915-1-1.html
我问过这个问题 呵呵


尤其是age*34是干什么用的。
老师视频里有讲,  不管是34  还是37  还是29 都可以。这里是为了让 姓名 和年龄 相加 不一样  比如 假如姓名  20  年龄 40   另一个人 姓名 40  年龄 20  这不就一样了么。你让年龄乘以一个数就好了。。
回复 使用道具 举报
本帖最后由 刘学明    于 2013-5-16 12:06 编辑
  1. package day16;
  2. import java.util.*;


  3. class Student11 implements Comparable<Student11>//实现一个比较方法。
  4. {
  5.         private String name;
  6.         private int age;
  7.         Student11(String name,int age)
  8.         {
  9.                 this.name=name;
  10.                 this.age=age;
  11.         }
  12.         public int compareTo(Student11 s)//比较年龄大小。
  13.         {
  14.                 //下面句子是干啥用的。关键字new Integer.age不是已经是int了,为啥不直接比较却要装箱后比较。
  15.               回答: 装箱的目的是要使用CompareTo这个方法,CompareTo 这个方法才可以根据比较结果 返回 1 0 或者-1  如果不装箱没有这个方法,如果只用基本数据类型比较的话  比较 this.age == s.age  只会返回true或者false    但是CompareTo方法需要的是返回int类型来决定排序。  这也是装箱的好处!               
  16. int num=new Integer(this.age).compareTo(new Integer(s.age));
  17.                 if(num==0)
  18.                         return this.name.compareTo(name);
  19.                 return num;
  20.         }
  21.         public int hashCode()//这个方法是返回此映射到hash码值。但是这又有什么作用呢?在该例子中,不是太明白。尤其是age*34是干什么用。
  22.         {
  23.                 return name.hashCode()+age*34;    age之所以乘以34是为了避免遇到哈希值相同的情况 *34以后遇到相同的情况就大大减小了 不一定是*34 也可以* 27 ,*47等等都可以
  24.         }
  25.         public boolean equals(Object obj)//覆盖一个对象。是否是学生类对象。
  26.         {
  27.                 if(!(obj instanceof Student11))
  28.                         throw new ClassCastException("class not macth");
  29.                 Student11 s=(Student11)obj;
  30.                 return this.name.equals(s.name)&&this.age==age;
  31.         }
  32.         public String getName()
  33.         {
  34.                 return name;
  35.         }
  36.         public int getAge()
  37.         {
  38.                 return age;
  39.         }
  40.         public String toString()
  41.         {
  42.                 return name+age;
  43.         }
  44. }
  45. public class MapTest {

  46.         /**
  47.          * @param args
  48.          */
  49.         public static void main(String[] args) {
  50.                 // TODO Auto-generated method stub
  51.                 HashMap<Student11, String> hm=new HashMap<Student11,String>();
  52.                 hm.put(new Student11("lisi1",21), "beijing");
  53.                 hm.put(new Student11("lisi2",22), "shanghai");
  54.                 hm.put(new Student11("lisi3",23), "tianjing");
  55.                 hm.put(new Student11("lisi3",23), "tianjing");
  56.                 hm.put(new Student11("lisi4",24), "wuhan");
  57.                
  58.                 //第一种取出方式
  59.                 Set<Student11> keySet=hm.keySet();
  60.                 Iterator<Student11>it=keySet.iterator();
  61.                 while(it.hasNext())
  62.                 {
  63.                         Student11 stu=it.next();
  64.                         String add=hm.get(stu);
  65.                         System.out.println(add+stu);
  66.                 }
  67.                 //第二中取出方式
  68.                 Set<Map.Entry<Student11, String>> entrySet=hm.entrySet();
  69.                 Iterator<Map.Entry<Student11, String>> iter=entrySet.iterator();
  70.                 while(iter.hasNext())
  71.                 {
  72.                         Map.Entry<Student11, String> me=iter.next();
  73.                         Student11 stu=me.getKey();
  74.                         String addr=me.getValue();
  75.                         System.out.println(stu+";;"+addr);
  76.                 }
  77.                
  78.                
  79.                
  80.                  
  81.         }

  82. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
袁梦希 + 1

查看全部评分

回复 使用道具 举报
知道了,谢谢哦
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马