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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Johnny_Hu 中级黑马   /  2015-4-14 20:10  /  947 人查看  /  5 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 Johnny_Hu 于 2015-4-14 20:15 编辑

   
  1. import java.util.*;
  2.       
  3. public class MapDemo1 {
  4.       
  5.     public static void main(String[] args)
  6.     {
  7.         Map<Student, String> stuMap = new HashMap<Student, String>();
  8.         stuMap.put(new Student("胡平燕1", 21),"湖南1");
  9.         stuMap.put(new Student("胡平燕2", 23),"湖南2");
  10.         stuMap.put(new Student("胡平燕3", 50),"湖南4");
  11.         stuMap.put(new Student("胡平燕3", 29),"湖南4");
  12.         stuMap.put(new Student("胡平燕3", 29),"湖南3");
  13.       
  14.         //keySet取出方式
  15.         Set<Student> keySet = stuMap.keySet();
  16.         for(Iterator<Student> it = keySet.iterator(); it.hasNext();)
  17.         {
  18.             Student key = it.next();
  19.             String name= stuMap.get(key);
  20.             System.out.println(key.getName()+" "+key.getAge()+" "+name);            
  21.         }
  22.         //entrySet取出方式
  23.         Set<Map.Entry<Student, String>> entrySet = stuMap.entrySet();
  24.         for(Iterator<Map.Entry<Student, String>> iter = entrySet.iterator(); iter.hasNext();)
  25.         {
  26.             Map.Entry<Student, String> me = iter.next();
  27.             Student stu = me.getKey();
  28.             String addr = me.getValue();
  29.             System.out.println(stu.getName()+"----"+stu.getAge()+"----"+addr);         
  30.         }
  31.     }
  32.       
  33. }
  34.       
  35. class Student implements Comparable<Student>
  36. {
  37.     private String name;
  38.     private int age;
  39.     Student(String name,int age)
  40.     {
  41.         this.name = name;
  42.         this.age = age;
  43.     }
  44.     public int getAge()
  45.     {
  46.         return age;
  47.     }
  48.     public String getName()
  49.     {
  50.         return name;
  51.     }
  52.     @Override
  53.     public int compareTo(Student s)
  54.     {
  55.         int num = new Integer(this.age).compareTo(new Integer(s.age));
  56.         if(num == 0)
  57.             return this.name.compareTo(s.name);
  58.         return num;
  59.     }
  60.       
  61.     public int hashCode()
  62.     {
  63.         return this.name.hashCode()+age*13;
  64.     }
  65.       
  66.     public boolean equals(Object obj)
  67.     {
  68.         if(!(obj instanceof Student))
  69.             throw new ClassCastException("类型不匹配");
  70.         Student s = (Student)obj;
  71.         //下面这两句 不管我写哪句 程序输出正确 没报错
  72.         //哪位大神可以帮我讲解下 它们会在什么情况下有区别呢
  73.         return this.getName().equals(s.getName()) && this.getAge() == s.getAge();
  74.         return this.name.equals(s.name) && this.age == s.age;
  75.     }   
  76. }
复制代码

//下面这两句 不管我写哪句 程序输出正确 没报错  
       //哪位大神可以帮我讲解下 它们会在什么情况下有区别呢
        return this.getName().equals(s.getName()) && this.getAge() == s.getAge();
        return this.name.equals(s.name) && this.age == s.age;
上面代码块中也有标注
哪位大神可以帮我讲解下 它们会在什么情况下有区别呢

评分

参与人数 1技术分 +1 收起 理由
lwj123 + 1 很给力!

查看全部评分

5 个回复

倒序浏览
你的意思是说compareTo的问题吗?
我的理解是这样的,你是不管输入这两句话任意一句都没有用是吧:
return this.getName().equals(s.getName()) && this.getAge() == s.getAge();
         return this.name.equals(s.name) && this.age == s.age;
那你有考虑compareTo方法被执行了?
答案是没有被执行的。。。。
不信,你可以在compareTo方法打个log试试。
回复 使用道具 举报
lwj123 发表于 2015-4-14 20:22
你的意思是说compareTo的问题吗?
我的理解是这样的,你是不管输入这两句话任意一句都没有用是吧:
return ...

不是那个问题  我想到什么原因了
是我看毕老师讲的课程是有些地方写:
return this.name.compareTo(s.name);
而有些地方呢就写
p1.getName().compareTo(p2.getName());

是因为name跟age这两个变量我私有了 只能在本类中调用
而第一种写法 是直接写在 本类中的
第二种呢  是写在比较器里面的  
比较器要另外新建个类
所有需要用到get方法来调用

也非常谢谢你的耐心解答
回复 使用道具 举报
主要是在,若类中成员变量用private只能用
return this.getName().equals(s.getName()) && this.getAge() == s.getAge();
若是没有用private修饰,则没有什么太多不同~~
回复 使用道具 举报
学习了,楼主细节挖的很深!
回复 使用道具 举报
小丸子 发表于 2015-4-14 22:15
学习了,楼主细节挖的很深!

我也是突然想到权限这点的  :):)
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马