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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© android0276 中级黑马   /  2014-7-6 20:20  /  3659 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

例1:HashMap中是通过比较hashCode()和equals()方法来判断key值是否会重复,如果重复,则会覆盖先前的value。
注意:这里如果是HashSet集合,向其中添加重复的元素(返回false),则添加失败,不会覆盖。

  1. package com.itheima;

  2. import java.util.Comparator;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.Set;
  6. import java.util.TreeMap;

  7. public class TreeMapTest {
  8.         public static void main(String[] args) {

  9.                 HashMap<Student,String> hm = new HashMap<Student,String>();
  10.                 hm.put(new Student("liu",20), "liu");
  11.                 hm.put(new Student("xiao",19), "xiao");
  12.                 hm.put(new Student("lulu",20), "lulu1");
  13.                 hm.put(new Student("lulu",20), "lulu2");

  14.                 Set<Student> set = hm.keySet();
  15.                 for(Iterator<Student> it = set.iterator();it.hasNext();){
  16.                         Student s = it.next();
  17.                         String value = hm.get(s);
  18.                         System.out.println("key--"+s.getName()+","+s.getAge()+",value--"+value);
  19.                 }
  20.         }

  21. }
  22. class LenComparator implements Comparator<Student>{

  23.         @Override
  24.         public int compare(Student s1, Student s2) {
  25.                 // TODO Auto-generated method stub
  26.                 int num = new Integer(s1.getName().length()).compareTo(s2.getName().length());
  27.                 if(num == 0)
  28.                         return new Integer(s1.getAge()).compareTo(s2.getAge());
  29.                 return num;
  30.         }
  31.        
  32. }
  33. class Student implements Comparable<Student>{
  34.         private String name;
  35.         private int age;
  36.         Student(String name,int age){
  37.                 this.name = name;
  38.                 this.age = age;
  39.         }
  40.         public String getName() {
  41.                 return name;
  42.         }
  43.         public void setName(String name) {
  44.                 this.name = name;
  45.         }
  46.         @Override
  47.         public int hashCode(){
  48.                 System.out.println("aaa");
  49.                 return name.hashCode()+age*27;
  50.         }
  51.         @Override
  52.         public boolean equals(Object o){
  53.                 if(o instanceof Student){
  54.                         Student s = (Student)o;
  55.                         System.out.println(this.getName()+" equals"+"---"+s.getName());
  56.                         if(this.age == s.getAge() && this.name .equals( s.getName()))
  57.                                 return true;
  58.                         return false;
  59.                 }
  60.                 throw new ClassCastException("类型不匹配");
  61.         }
  62.         public int getAge() {
  63.                 return age;
  64.         }
  65.         public void setAge(int age) {
  66.                 this.age = age;
  67.         }
  68.         @Override
  69.         public int compareTo(Student s) {
  70.                 // TODO Auto-generated method stub
  71.                 int num = new Integer(this.getAge()).compareTo(s.getAge());
  72.                 if(num == 0)
  73.                         return new Integer(this.getName().length()).compareTo(s.getName().length());
  74.                 return num;
  75.                
  76.         }
  77. }
复制代码

现后台打印了hashCode()和equals()里面的输出语句,因此调用了hashCode()方法和equals方法。上面的例子Student不需要实现Comparable<Student>接口。
例2:TreeMap中是如何实现key值唯一的列?

  1. package com.itheima;

  2. import java.util.Comparator;
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.Set;
  6. import java.util.TreeMap;

  7. public class TreeMapTest {
  8.         public static void main(String[] args) {
  9. //                TreeMap<Student,String> tm = new TreeMap<Student,String>(new LenComparator());
  10.                 TreeMap<Student,String> tm = new TreeMap<Student,String>();
  11.                 tm.put(new Student("liu",20), "liu");
  12.                 tm.put(new Student("xiao",19), "xiao");
  13.                 tm.put(new Student("lulu",20), "lulu1");
  14.                 tm.put(new Student("lulu",20), "lulu2");
  15.                 Set<Student> set = tm.keySet();
  16.                 for(Iterator<Student> it = set.iterator();it.hasNext();){
  17.                         Student s = it.next();
  18.                         String value = tm.get(s);
  19.                         System.out.println("key--"+s.getName()+","+s.getAge()+",value--"+value);
  20.                 }
  21.         }

  22. }
  23. class LenComparator implements Comparator<Student>{

  24.         @Override
  25.         public int compare(Student s1, Student s2) {
  26.                 // TODO Auto-generated method stub
  27.                 int num = new Integer(s1.getName().length()).compareTo(s2.getName().length());
  28.                 if(num == 0)
  29.                         return new Integer(s1.getAge()).compareTo(s2.getAge());
  30.                 return num;
  31.         }
  32.        
  33. }
  34. class Student implements Comparable<Student>{
  35.         private String name;
  36.         private int age;
  37.         Student(String name,int age){
  38.                 this.name = name;
  39.                 this.age = age;
  40.         }
  41.         public String getName() {
  42.                 return name;
  43.         }
  44.         public void setName(String name) {
  45.                 this.name = name;
  46.         }
  47.         @Override
  48.         public int hashCode(){
  49.                 System.out.println("aaa");
  50.                 return name.hashCode()+age*27;
  51.         }
  52.         @Override
  53.         public boolean equals(Object o){
  54.                 if(o instanceof Student){
  55.                         Student s = (Student)o;
  56.                         System.out.println(this.getName()+" equals"+"---"+s.getName());
  57.                         if(this.age == s.getAge() && this.name .equals( s.getName()))
  58.                                 return true;
  59.                         return false;
  60.                 }
  61.                 throw new ClassCastException("类型不匹配");
  62.         }
  63.         public int getAge() {
  64.                 return age;
  65.         }
  66.         public void setAge(int age) {
  67.                 this.age = age;
  68.         }
  69.         @Override
  70.         public int compareTo(Student s) {
  71.                 // TODO Auto-generated method stub
  72.                 int num = new Integer(this.getAge()).compareTo(s.getAge());
  73.                 if(num == 0)
  74.                         return new Integer(this.getName().length()).compareTo(s.getName().length());
  75.                 return num;
  76.                
  77.         }
  78. }
复制代码

结果发现,tm.put(new Student("lulu",20), "lulu1");tm.put(new Student("lulu",20), "lulu2");后者的key覆盖掉了前面的key,
但是没有调用hashCode()方法和equals()方法。
但是如果不重写父类的hashCode()方法和equals()方法,后者的key仍然会覆盖前面的key.
因此,TreeMap底层是如何确定key值唯一的列?

评分

参与人数 1技术分 +1 收起 理由
李小然 + 1

查看全部评分

1 个回复

倒序浏览
Map 接口是按照 equals 操作定义的,但有序映射使用它的 compareTo(或 compare)方法对所有键进行比较,因此从有序映射的观点来看,此方法认为相等的两个键就是相等的。即使排序与 equals 不一致,有序映射的行为仍然是 定义良好的,只不过没有遵守 Map 接口的常规协定。 TreeMap意味着按照元素的自然顺序排序,与TreeSet一样,当构造TreeMap时,允许定义自己的排序顺序(通过实现接口Comparable或者Comparator),以指定元素排序时它们相互之间应该如何进行比较。

评分

参与人数 1技术分 +1 收起 理由
李小然 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马