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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

  1. import java.util.*;
  2. //学生类
  3. class Student implements Comparable<Student>
  4. {
  5.         private String name;
  6.         private int age;

  7.         //构造函数初始化学生
  8.         Student(String name,int age)
  9.         {
  10.                 this.name = name;
  11.                 this.age = age;
  12.         }

  13.         public int compareTo(Student stu)
  14.         {
  15.                 //按学生的年龄排序,如果年龄相等就按姓名字典序排序
  16.                 int num = new Integer(this.getAge()).compareTo(new Integer(stu.getAge()));

  17.                 if(num == 0)
  18.                         return this.getName().compareTo(stu.getName());

  19.                 //否则就按年龄排序
  20.                 return num;
  21.         }

  22.         //计算学生对象的哈希地址
  23.         public int hashCode()
  24.         {
  25.                 return name.hashCode()+age*37;
  26.         }

  27.         public boolean equals(Object obj)
  28.         {
  29.                 //如果不是学生对象,抛出类型转换异常
  30.                 if(!(obj instanceof Student))
  31.                         throw new ClassCastException("不能转换成Student类型");

  32.                 //类型转换
  33.                 Student stu = (Student)obj;

  34.                 //比较学生对象姓名和年龄,去除重复元素
  35.                 return this.getName().equals(stu.getName()) && this.getAge()==stu.getAge();
  36.         }

  37.         public void setName(String name)
  38.         {
  39.                 this.name =name;
  40.         }

  41.         public String getName()
  42.         {
  43.                 return name;
  44.         }

  45.         public void setAge(int age)
  46.         {
  47.                 this.age = age;
  48.         }

  49.         public int getAge()
  50.         {
  51.                 return age;
  52.         }

  53.         public String toString()
  54.         {
  55.                 return name+"....."+age;
  56.         }
  57. }

  58. //主类用public修饰
  59. public class MapTest
  60. {
  61.         public static void sop(Object obj)
  62.         {
  63.                 System.out.println(obj);
  64.         }

  65.         public static void main(String[] args)
  66.         {
  67.                 //1 创建hashMap映射
  68.                 HashMap<Student,String> hm = new HashMap<Student,String>();

  69.                 //2 添加学生对象为键和对应的地址为值
  70.                 hm.put(new Student("Jack",20),"北京");
  71.                 hm.put(new Student("mike",19),"兰州");
  72.                 hm.put(new Student("mike",19),"兰州");
  73.                 hm.put(new Student("Tom",25),"广州");
  74.                 hm.put(new Student("Alice",17),"上海");
  75.                 hm.put(new Student("Jimmy",21),"长沙");

  76.                 //3 取出学生和对应地址
  77. getWay_2(hm);
  78.         }

  79.         //3.2 第二种方式从Map中取出所有键值
  80.         public static void getWay_2(HashMap<Student,String> hm)
  81.         {

  82.                 sop("第二种方式从Map中取出所有键值");

  83.                 //1 把所有的映射关系存入Set<Map.Entry<Student,String>>
  84.                 Set<Map.Entry<Student,String>> entrySet = hm.entrySet();

  85.                 //2 获取迭代器
  86.                 for(Iterator<Map.Entry<Student,String>> it = entrySet.iterator(); it.hasNext(); )
  87.                 {
  88.                         //3 迭代取出映射项
  89.                         Map.Entry<Student,String> me = it.next();

  90.                         //4 获取学生
  91.                         Student stu = me.getKey();
  92.                         //5 获取地址
  93.                         String addr = me.getValue();

  94.                         //6打印获取结果
  95.                         sop(stu.toString()+"......"+addr);
  96.                 }       
  97.         }
  98. }
复制代码


第二种方式.jpg (47.7 KB, 下载次数: 6)

第二种方式.jpg

4 个回复

正序浏览
还没看到这里。顶一下。

点评

谢谢  发表于 2015-4-18 21:42
回复 使用道具 举报
:lol赞一个 棒棒哒

点评

谢谢  发表于 2015-4-18 21:45
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马