黑马程序员技术交流社区
标题:
TreeMap中是如何实现key值唯一的?
[打印本页]
作者:
android0276
时间:
2014-7-6 20:20
标题:
TreeMap中是如何实现key值唯一的?
例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值唯一的列?
作者:
導ぷ仙″兲蕐
时间:
2014-7-6 21:19
Map 接口是按照 equals 操作定义的,但有序映射使用它的 compareTo(或 compare)方法对所有键进行比较,因此从有序映射的观点来看,此方法认为相等的两个键就是相等的。即使排序与 equals 不一致,有序映射的行为仍然是 定义良好的,只不过没有遵守 Map 接口的常规协定。 TreeMap意味着按照元素的自然顺序排序,与TreeSet一样,当构造TreeMap时,允许定义自己的排序顺序(通过实现接口Comparable或者Comparator),以指定元素排序时它们相互之间应该如何进行比较。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2