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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 短板 中级黑马   /  2015-5-9 00:11  /  703 人查看  /  13 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.util.* ;
  2. class MapTest
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 HashMap<Student,String> hp = new HashMap<Student,String>();
  7.                 hp.put(new Student("LP01",15),"bj");
  8.                 hp.put(new Student("LP02",15),"sh");
  9.                 hp.put(new Student("LP03",15),"nj");
  10.                 hp.put(new Student("LP03",15),"tj");
  11.                 hp.put(new Student("LP04",15),"hah");
  12.                
  13.                 Set<Map.Entry<Student,String>> entrySet = hp.entrySet();
  14.                 for (Iterator<Map.Entry<Student,String>> iter =entrySet.iterator();iter.hasNext() ; )
  15.                 {
  16.                         Map.Entry<Student,String> me = iter.next();
  17.                         Student s = me.getKey();
  18.                         String ads = me.getValue();
  19.                         System.out.println(s+"________"+ads);
  20.                 }
  21.         }
  22. }
  23. class Student implements Comparable<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.         public int hashCode()
  41.         {
  42.                 return name.hashCode()+age*13 ;
  43.         }
  44.         public boolean equals(Object obj)
  45.         {
  46.                 if (!(obj instanceof Student))
  47.                         throw new ClassCastException("该对象不是学生");
  48.                 Student stu =(Student)obj;
  49.                 return this.name.equals(stu.name)&&this.age==stu.age;
  50.         }
  51.         public int compareTo(Student s)
  52.         {
  53.                 int num = new Integer(this.age).compareTo(new Integer(s.age));
  54.                 if (num==0)
  55.                         return this.name.compareTo(s.name);
  56.                 return num ;
  57.         }
  58. }
复制代码


13 个回复

倒序浏览
cmd输出

Person@45e65f4_________深圳
Person@45e65e5_________上海
Person@45e65d7_________南京
Person@45e65f3_________广州
Person@45e65f4...深圳
Person@45e65e5...上海
Person@45e65d7...南京
Person@45e65f3...广州

我的人类为何是这样的

求大神指教
回复 使用道具 举报
Person@45e65f4_________深圳
Person@45e65e5_________上海
Person@45e65d7_________南京
Person@45e65f3_________广州
Person@45e65f4...深圳
Person@45e65e5...上海
Person@45e65d7...南京
Person@45e65f3...广州
回复 使用道具 举报

D:\Java\Javatest\day16>javac MapTest.java

D:\Java\Javatest\day16>java MapTest
Student@23bf48________bj
Student@23bf49________sh
Student@23bf4a________tj
Student@23bf4b________hah

是这样的 ~~~~~~~~~
回复 使用道具 举报
没复写Student类的toString方法输出时调用应该就行了
回复 使用道具 举报
存入的是对象的引用,打印出来就是对象的引用地址。
回复 使用道具 举报
你打印出来的只是在集合中的地址值,还要转换成String进行输出才可以呀
回复 使用道具 举报
你这样输出的是对象的引用,输出的是对象的内存地址,你在Student类中复写toString()方法就可以了,比如:
  1. public String toString(){
  2.                         return "{name:"+name+",age:"+age+"}";
  3.                 }
复制代码
回复 使用道具 举报
上面说的对,Student类需要覆盖父类的 toString()方法,这样直接调用对象的引用时就能显示toString()方法中返回的值了

Public  String toString(){
    return "姓名:"+this.name+" 年龄:"+this.age;
}
回复 使用道具 举报
痲ボㄋ. 发表于 2015-5-9 09:46
你这样输出的是对象的引用,输出的是对象的内存地址,你在Student类中复写toString()方法就可以了,比如:
...

哦  多谢
回复 使用道具 举报
想要那片海 发表于 2015-5-9 08:35
你打印出来的只是在集合中的地址值,还要转换成String进行输出才可以呀

对哦 谢谢了
回复 使用道具 举报
紫夜灵魂 发表于 2015-5-9 09:59
上面说的对,Student类需要覆盖父类的 toString()方法,这样直接调用对象的引用时就能显示toString()方法中 ...

哦哦 谢谢
回复 使用道具 举报
姓名 :年龄:性别:
回复 使用道具 举报
复写 toString 方法就可以了。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马