例1:HashMap中是通过比较hashCode()和equals()方法来判断key值是否会重复,如果重复,则会覆盖先前的value。
注意:这里如果是HashSet集合,向其中添加重复的元素(返回false),则添加失败,不会覆盖。
- package com.itheima;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Set;
- import java.util.TreeMap;
- public class TreeMapTest {
- public static void main(String[] args) {
- HashMap<Student,String> hm = new HashMap<Student,String>();
- hm.put(new Student("liu",20), "liu");
- hm.put(new Student("xiao",19), "xiao");
- hm.put(new Student("lulu",20), "lulu1");
- hm.put(new Student("lulu",20), "lulu2");
- Set<Student> set = hm.keySet();
- for(Iterator<Student> it = set.iterator();it.hasNext();){
- Student s = it.next();
- String value = hm.get(s);
- System.out.println("key--"+s.getName()+","+s.getAge()+",value--"+value);
- }
- }
- }
- class LenComparator implements Comparator<Student>{
- @Override
- public int compare(Student s1, Student s2) {
- // TODO Auto-generated method stub
- int num = new Integer(s1.getName().length()).compareTo(s2.getName().length());
- if(num == 0)
- return new Integer(s1.getAge()).compareTo(s2.getAge());
- return num;
- }
-
- }
- class Student implements Comparable<Student>{
- private String name;
- private int age;
- Student(String name,int age){
- this.name = name;
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public int hashCode(){
- System.out.println("aaa");
- return name.hashCode()+age*27;
- }
- @Override
- public boolean equals(Object o){
- if(o instanceof Student){
- Student s = (Student)o;
- System.out.println(this.getName()+" equals"+"---"+s.getName());
- if(this.age == s.getAge() && this.name .equals( s.getName()))
- return true;
- return false;
- }
- throw new ClassCastException("类型不匹配");
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- @Override
- public int compareTo(Student s) {
- // TODO Auto-generated method stub
- int num = new Integer(this.getAge()).compareTo(s.getAge());
- if(num == 0)
- return new Integer(this.getName().length()).compareTo(s.getName().length());
- return num;
-
- }
- }
复制代码
现后台打印了hashCode()和equals()里面的输出语句,因此调用了hashCode()方法和equals方法。上面的例子Student不需要实现Comparable<Student>接口。
例2:TreeMap中是如何实现key值唯一的列?
- package com.itheima;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Set;
- import java.util.TreeMap;
- public class TreeMapTest {
- public static void main(String[] args) {
- // TreeMap<Student,String> tm = new TreeMap<Student,String>(new LenComparator());
- TreeMap<Student,String> tm = new TreeMap<Student,String>();
- tm.put(new Student("liu",20), "liu");
- tm.put(new Student("xiao",19), "xiao");
- tm.put(new Student("lulu",20), "lulu1");
- tm.put(new Student("lulu",20), "lulu2");
- Set<Student> set = tm.keySet();
- for(Iterator<Student> it = set.iterator();it.hasNext();){
- Student s = it.next();
- String value = tm.get(s);
- System.out.println("key--"+s.getName()+","+s.getAge()+",value--"+value);
- }
- }
- }
- class LenComparator implements Comparator<Student>{
- @Override
- public int compare(Student s1, Student s2) {
- // TODO Auto-generated method stub
- int num = new Integer(s1.getName().length()).compareTo(s2.getName().length());
- if(num == 0)
- return new Integer(s1.getAge()).compareTo(s2.getAge());
- return num;
- }
-
- }
- class Student implements Comparable<Student>{
- private String name;
- private int age;
- Student(String name,int age){
- this.name = name;
- this.age = age;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public int hashCode(){
- System.out.println("aaa");
- return name.hashCode()+age*27;
- }
- @Override
- public boolean equals(Object o){
- if(o instanceof Student){
- Student s = (Student)o;
- System.out.println(this.getName()+" equals"+"---"+s.getName());
- if(this.age == s.getAge() && this.name .equals( s.getName()))
- return true;
- return false;
- }
- throw new ClassCastException("类型不匹配");
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- @Override
- public int compareTo(Student s) {
- // TODO Auto-generated method stub
- int num = new Integer(this.getAge()).compareTo(s.getAge());
- if(num == 0)
- return new Integer(this.getName().length()).compareTo(s.getName().length());
- return num;
-
- }
- }
复制代码
结果发现,tm.put(new Student("lulu",20), "lulu1");tm.put(new Student("lulu",20), "lulu2");后者的key覆盖掉了前面的key,
但是没有调用hashCode()方法和equals()方法。
但是如果不重写父类的hashCode()方法和equals()方法,后者的key仍然会覆盖前面的key.
因此,TreeMap底层是如何确定key值唯一的列? |