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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© itheima_llt 高级黑马   /  2015-4-19 11:52  /  337 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 itheima_llt 于 2015-4-19 11:54 编辑

TreeMap自定义对象存取练习需求:每一个学生都有对应的归属地。学生Student,地址String。学生属性:姓名,年龄。
注意:姓名和年龄相同的视为同一个学生。保证学生的唯一性。
对学生对象的年龄进行升序排序,用Comparable实现;对学生的姓名进行字典序排序,用Comparator实现。
因为数据是以键值对形式存在的。所以要使用可以排序的Map集合。
TreeMap。
思路:
1 把学生属性存入学生对象,并抽象出学生类
2 创建TreeMap容器,并把学生对象存入
3 取出学生
4 创建比较器类并测试按姓名字典序排序在写学生类要注意的地方:
1 实现Comparable接口,覆盖compareTo方法
2 去重,计算哈希值,覆盖hashCode和equals方法3
覆盖toString方法
4 记得写全set ,get方法
  1. import java.util.*;
  2. //主类
  3. public class TreeMapTest
  4. {
  5.         public static void sop(Object obj)
  6.         {
  7.                 System.out.println(obj);
  8.         }

  9.         public static void main(String[] args)
  10.         {
  11.                 //1 创建TreeMap映射对象
  12.                 //TreeMap<Student,String> tm = new TreeMap<Student,String>(new StuNameComparator());//姓名字典序排序
  13.                 TreeMap<Student,String> tm = new TreeMap<Student,String>();//年龄升序排序

  14.                 //2 添加Student对象
  15.                 tm.put(new Student("Jack",25),"北京");
  16.                 tm.put(new Student("Jack",22),"北京");
  17.                 tm.put(new Student("Mike",22),"天津");
  18.                 //sop(tm.put(new Student("Mike",22),"淮南"));//注意了,如果Student相同,但是地址不相同,根据put方法,新地址会覆盖原来的地址
  19.                 tm.put(new Student("Jimmy",21),"上海");
  20.                 tm.put(new Student("Jakie",20),"广州");
  21.                 tm.put(new Student("David",17),"长沙");
  22.                 tm.put(new Student("Tom",26),"连云港");

  23.                 //第一种方式取出所有键值
  24.                 sop("第一种方式取出所有键值");
  25.                  getWay(tm);

  26.                 //第二种方式取出所有键值
  27.                 //sop("第二种方式取出所有键值");
  28.                  //getWay_2(tm);
  29.         }

  30.         //第一种方式迭代取出所有键值
  31.         public static void getWay(TreeMap<Student,String> tm)
  32.         {
  33.                 //1 把所有key存入set
  34.                 Set<Student> keySet = tm.keySet();

  35.                 //2 获取迭代器
  36.                 for(Iterator<Student> it = keySet.iterator(); it.hasNext(); )
  37.                 {
  38.                         //3 迭代取出key
  39.                         Student stu = it.next();
  40.                         //4 通过key获取value
  41.                         String addr = tm.get(stu);

  42.                         //打印取出结果
  43.                         sop(stu.toString()+".........."+addr);
  44.                 }
  45.         }

  46.         //第二种方式迭代取出所有键值
  47.         public static void getWay_2(TreeMap<Student,String> tm)
  48.         {
  49.                 //1 把所有映射项存入Map.Entry
  50.                 Set<Map.Entry<Student,String>> entry = tm.entrySet();

  51.                 //2 获取迭代器
  52.                 for(Iterator<Map.Entry<Student,String>> it = entry.iterator(); it.hasNext(); )
  53.                 {
  54.                         //3 迭代取出映射项
  55.                         Map.Entry<Student,String> me = it.next();

  56.                         //4 利用Map.Entry中的get方法获取key和value
  57.                         Student stu = me.getKey();
  58.                         String addr = me.getValue();

  59.                         //5打印取出结果
  60.                         sop(stu.toString()+"***********"+addr);
  61.                 }
  62.         }
  63. }

  64. //学生姓名比较器类
  65. class StuNameComparator implements Comparator<Student>
  66. {
  67.         //覆盖compare方法
  68.         public int compare(Student stu1,Student stu2)
  69.         {
  70.                 //先按姓名排序,如果相同再按年龄升序排序
  71.                 int num = stu1.getName().compareTo(stu2.getName());

  72.                 if(num == 0)
  73.                         return new Integer(stu1.getAge()).compareTo(new Integer(stu2.getAge()));
  74.                
  75.                 //否则就按按姓名排序
  76.                 return  num;
  77.         }
  78. }


  79. //学生类
  80. class Student implements Comparable<Student>
  81. {
  82.         private String name;
  83.         private int age;

  84.         //构造函数初始化学生
  85.         Student(String name,int age)
  86.         {
  87.                 this.name = name;
  88.                 this.age = age;
  89.         }

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

  94.                 if(num == 0)
  95.                         return this.getName().compareTo(stu.getName());

  96.                 //否则就按年龄排序
  97.                 return num;
  98.         }

  99.         //计算学生对象的哈希地址
  100.         public int hashCode()
  101.         {
  102.                 return name.hashCode()+age*37;
  103.         }

  104.         public boolean equals(Object obj)
  105.         {
  106.                 //如果不是学生对象,抛出类型转换异常
  107.                 if(!(obj instanceof Student))
  108.                         throw new ClassCastException("不能转换成Student类型");

  109.                 //类型转换
  110.                 Student stu = (Student)obj;

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

  114.         public void setName(String name)
  115.         {
  116.                 this.name =name;
  117.         }

  118.         public String getName()
  119.         {
  120.                 return name;
  121.         }

  122.         public void setAge(int age)
  123.         {
  124.                 this.age = age;
  125.         }

  126.         public int getAge()
  127.         {
  128.                 return age;
  129.         }

  130.         public String toString()
  131.         {
  132.                 return name+"....."+age;
  133.         }
  134. }
复制代码
注意以下操作均在主类的主函数进行。
如果想要第一种或者第二种方式迭代取出所有键值,请打开或者关闭对于注释语句。
如果想按姓名字典序排序,请打开对应语句,并关闭按年龄升序排序语句;
如果想按年龄升序排序,请打开对应语句,并关闭姓名字典序排序。
当存入重复的学生,不同的地址时候,put方法会返回被覆盖的地址!




0 个回复

您需要登录后才可以回帖 登录 | 加入黑马