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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 changchunhaha 于 2014-4-3 14:39 编辑
  1. import java.util.*;

  2. class MapEntry
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 Map<Student,String> m = new HashMap<Student,String>();

  7.                 m.put(new Student("zhangsan01",21),"shanghai");
  8.                 m.put(new Student("zhangsan02",22),"beijing");
  9.                 m.put(new Student("zhangsan03",23),"wuhan");
  10.                 m.put(new Student("zhangsan04",24),"tianjin");
  11.                
  12.                 Set<Map.Entry<Student,String>> sme = m.entrySet();//我不明白泛型在这是怎么应用的?求大神帮助!!

  13.                 Iterator<Map.Entry<Student,String>> it = sme.iterator();//我不明白泛型在这是怎么应用的?

  14.                 while(it.hasNext())
  15.                 {
  16.                         Map.Entry<Student,String> me = it.next();
  17.                         Student stu = me.getKey();
  18.                         String addr = me.getValue();
  19.                         System.out.println(stu+"............"+addr);
  20.                 }
  21.         }
  22. }

  23. class Student
  24. {
  25.         private String name;
  26.         private int age;
  27.         Student(String name,int age)
  28.         {
  29.                 this.name=name;
  30.                 this.age=age;
  31.         }
  32.         public String getName()
  33.         {
  34.                 return name;
  35.         }
  36.         public int getAge()
  37.         {
  38.                 return age;
  39.         }
  40.         
  41.         public int hashCode()
  42.         {
  43.                 return name.hashCode()+age*30;
  44.         }
  45.         
  46.         public boolean equals(Object obj)
  47.         {
  48.                 if(!(obj instanceof Student))
  49.                         throw new ClassCastException("类型不匹配");
  50.                
  51.                 Student s = (Student)obj;
  52.                 return this.name.equals(s.name) && this.age==s.age;
  53.         }
  54.         
  55.         public int compareTo(Student s)  //还有这个compareTo的代码不是太明白,有点读不懂,谁能帮忙说一下原理走向。谢谢!!!
  56.         {
  57.                 int num = new Integer(this.age).compareTo(new Integer(s.age));
  58.                
  59.                 if(num==0)
  60.                         return this.name.compareTo(s.name);
  61.                 return num;
  62.         }
  63.         
  64.         public String toString()
  65.         {
  66.                 return name+"..."+age;
  67.         }
  68. }
复制代码


评分

参与人数 1技术分 +1 收起 理由
itpower + 1

查看全部评分

4 个回复

正序浏览
compareTo比较此对象与指定对象的顺序。如果该对象小于、等于或大于指定对象,则分别返回负整数、零或正整数所以该方法让对对象具有了比较性同样也可以重写该方法。
注意:此类具有与 equals 不一致的自然排序
回复 使用道具 举报
你这个类还得实现comparable的接口。compareTo是让对象具有比较性,你这方法的主条件是年龄,年龄相等的按姓名排序
回复 使用道具 举报
osully 发表于 2014-4-3 14:10
Set entrySet()   
map 的 entrySet()   这个方法返回类型是Map.Entry

谢谢!我就是有点没绕过来。
回复 使用道具 举报
Set<Map.Entry<K,V>> entrySet()   
map 的 entrySet()   这个方法返回类型是Map.Entry

而Map.Entry<K,V> 这里的kv 就是你map集合定义的泛型......
此类中有getkey和getvalue方法取出键值,

如果你要知道原理建议可以自己看下源代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马