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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 张扬123 中级黑马   /  2012-8-19 22:55  /  1540 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 张扬123 于 2012-8-20 15:38 编辑

不多说,先来代码。
  1. import java.util.*;
  2. class Student implements Comparable<Student>
  3. {
  4.         private String name;
  5.         private int age;
  6.         Student(String name,int age)
  7.         {
  8.                 this.name=name;
  9.                 this.age=age;
  10.         }
  11.         public int compareTo(Student s)
  12.         {
  13.                 int num = new Integer(this.age).compareTo(new Integer(s.age));
  14.                 if(num==0)
  15.                         return this.name.compareTo(s.name);
  16.                 return num;
  17.         }
  18.         public int hashCode()//一
  19.         {
  20.                 return name.hashCode()+age*34;
  21.         }
  22.         public boolean equals(Object obj)//二
  23.         {
  24.                 if(!(obj instanceof Student))
  25.                         throw new ClassCastException("类型不匹配");
  26.                 Student s = (Student)obj;

  27.                 return this.name.equals(s.name) && this.age==s.age;
  28.         }
  29.         public String getName()
  30.         {
  31.                 return name;
  32.         }
  33.         public int getAge()
  34.         {
  35.                 return age;
  36.         }
  37.         public String toString()
  38.         {
  39.                 return name+"::"+age;
  40.         }
  41. }
  42. class  MapTest
  43. {
  44.         public static void main(String[] args)
  45.         {
  46.                 HashMap<Student,String> hm = new HashMap<Student,String>();
  47.                 hm.put(new Student("lisi1",21),"beijing");
  48.                 hm.put(new Student("lisi1",21),"tianjin");
  49.                 hm.put(new Student("lisi2",22),"shanghai");
  50.                 hm.put(new Student("lisi3",23),"nanjing");
  51.                 hm.put(new Student("lisi4",24),"wuhan");

  52.                 Set<Student> keySet = hm.keySet();
  53.                 Iterator<Student> it = keySet.iterator();
  54.                 while (it.hasNext())
  55.                 {
  56.                         Student stu = it.next();
  57.                         String addr=hm.get(stu);
  58.                         System.out.println(stu+"..."+addr);
  59.                 }

  60.                 }
  61.         }
  62. }
复制代码
把上面的一,二处的hashCode和equals删除之后,也就是没有复写,会出现两个lisi1都输出的情况。
再看下面的。没有复写,就不会出现两个。
这是为什么?
  1. import java.util.*;
  2. class  MapDemo
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 Map<String,String> map = new HashMap<String,String>( );


  7. map.put("01","zhangsan1");
  8. map.put("01","wangwu");
  9. map.put("02","zhangsan2");
  10.                 map.put("03","zhangsan3");
  11.                
  12.                 Collection<String> coll=map.values();
  13.                 System.out.println(map);
  14.         }
  15. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
张_涛 + 1 赞一个!

查看全部评分

4 个回复

倒序浏览
因为你下面用的Map<Stirng,String>
你看看String的源码有没有覆写呢,它是覆盖了hashcode和equals方法吧
回复 使用道具 举报
你没有复写Person对象的hashCode方法跟equals方法,person对象存到哈希集合中是按默认的对象的哈希值来存的,虽然2个Person对象内容相同,但确是2个不同的对象,所以哈希值不同,也就不是重复元素,而字符串如果内容相同的话就视为2个相同的对象。
回复 使用道具 举报
对于HashMap集合,键的唯一性是通过作为键的元素对应的类中的hashCode和equals两个方法确定的,在向HashMap中添加元素的时候,首先会执行hashCode方法,如果返回的结果跟集合中其他元素执行hashCode后返回的结果相同,会自动执行equals方法,返回结果为true时,说明该键已存在,不再存入,只是把该键对应的值覆盖。
楼主删除Student类中的hashCode和equals两个方法后,向集合中添加元素时,会调用Object类中的hashCode方法,即使向集合添加貌似键相同的元素时,但他们执行hashCode返回的结果也不同,所以会出现两个lisi1。
而第二个例子,虽然没有手动的写hashCode和equals,但是向集合中添加的键是字符串,而String类覆盖了Object类的hashCode和equals方法,这两个方法能够确定字符串作为键时的唯一性,所以没有复写,也不会出现两个
回复 使用道具 举报
谢谢,已解决。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马