treeSet的顺序

来源:互联网 发布:mac其他文件怎么清理 编辑:程序博客网 时间:2024/06/06 03:43
[java] view plain copy
  1. import java.util.Set;  
  2. import java.util.TreeSet;  
  3.   
  4. public class Person{  
  5.     private String name;  
  6.     private int age;  
  7.     public Person(String name, int age){  
  8.         this.name = name;  
  9.         this.age = age;  
  10.     }  
  11.     public String toString(){  
  12.         return "姓名:" + this.name + ";年龄:" + this.age;  
  13.     }  
  14.       
  15.     public static void main(String[] args){  
  16.         Set<Person> allSet = new TreeSet<Person>();  
  17.         allSet.add(new Person("张三"30));  
  18.         allSet.add(new Person("李四"31));  
  19.         allSet.add(new Person("王五"32));  
  20.         allSet.add(new Person("王五"32));  
  21.         allSet.add(new Person("王五"33));  
  22.         allSet.add(new Person("赵六"33));  
  23.         allSet.add(new Person("孙七"33));  
  24.         System.out.println(allSet);  
  25.     }  
  26. }  

执行时出现以下错误:

Exception in thread "main" Java.lang.ClassCastException: Person cannot be cast t
o java.lang.Comparable
        at java.util.TreeMap.compare(Unknown Source)
        at java.util.TreeMap.put(Unknown Source)
        at java.util.TreeSet.add(Unknown Source)
        at Person.main(Person.java:17)

修改如下:继承Comparable接口,覆写compareTo()方法

[java] view plain copy
  1. import java.util.Set;  
  2. import java.util.TreeSet;  
  3.   
  4. public class Person implements Comparable<Person>{  
  5.     private String name;  
  6.     private int age;  
  7.     public Person(String name, int age){  
  8.         this.name = name;  
  9.         this.age = age;  
  10.     }  
  11.     public String toString(){  
  12.         return "姓名:" + this.name + ";年龄:" + this.age;  
  13.     }  
  14.       
  15.     public int compareTo(Person per){  
  16.         if(this.age < per.age){  
  17.             return 1;  
  18.         }else if(this.age > per.age){  
  19.             return -1;  
  20.         }else{  
  21.             return 0;  
  22.         }  
  23.     }  
  24.       
  25.     public static void main(String[] args){  
  26.         Set<Person> allSet = new TreeSet<Person>();  
  27.         allSet.add(new Person("张三"30));  
  28.         allSet.add(new Person("李四"31));  
  29.         allSet.add(new Person("王五"32));  
  30.         allSet.add(new Person("王五"32));  
  31.         allSet.add(new Person("王五"33));  
  32.         allSet.add(new Person("赵六"33));  
  33.         allSet.add(new Person("孙七"33));  
  34.         System.out.println(allSet);  
  35.     }  
  36. }  
此时完成了排序,但是年龄相同的元素没有了,继续修改。

[java] view plain copy
  1. import java.util.Set;  
  2. import java.util.TreeSet;  
  3.   
  4. public class Person implements Comparable<Person>{  
  5.     private String name;  
  6.     private int age;  
  7.     public Person(String name, int age){  
  8.         this.name = name;  
  9.         this.age = age;  
  10.     }  
  11.     public String toString(){  
  12.         return "姓名:" + this.name + ";年龄:" + this.age;  
  13.     }  
  14.       
  15.     public int compareTo(Person per){  
  16.         if(this.age < per.age){  
  17.             return 1;  
  18.         }else if(this.age > per.age){  
  19.             return -1;  
  20.         }else{  
  21.             return this.name.compareTo(per.name);   //调用String中的compareTo()方法  
  22.         }  
  23.     }  
  24.       
  25.     public static void main(String[] args){  
  26.         Set<Person> allSet = new TreeSet<Person>();  
  27.         allSet.add(new Person("张三"30));  
  28.         allSet.add(new Person("李四"31));  
  29.         allSet.add(new Person("王五"32));  
  30.         allSet.add(new Person("王五"32));  
  31.         allSet.add(new Person("王五"33));  
  32.         allSet.add(new Person("赵六"33));  
  33.         allSet.add(new Person("孙七"33));  
  34.         System.out.println(allSet);  
  35.     }  
  36. }  
此时并没有去掉重复的元素,要想去掉重复元素,则需要Object类中的两个方法帮助:

1.hashCode():表示一个唯一的编码,一般通过计算表示

2.equals():进行对象的比较操作

[java] view plain copy
  1. import java.util.Set;  
  2. import java.util.HashSet;  
  3.   
  4. public class Person{  
  5.     private String name;  
  6.     private int age;  
  7.     public Person(String name, int age){  
  8.         this.name = name;  
  9.         this.age = age;  
  10.     }  
  11.     public String toString(){  
  12.         return "姓名:" + this.name + ";年龄:" + this.age;  
  13.     }  
  14.       
  15.     public boolean equals(Object obj){  //覆写equals对象完成比较  
  16.         if(this == obj){  
  17.             return true;  
  18.         }  
  19.         if(!(obj instanceof Person)){  
  20.             return false;  
  21.         }  
  22.         Person p = (Person)obj;     //向下转型  
  23.         if(this.name.equals(p.name) && this.age == p.age){  
  24.             return true;  
  25.         }else{  
  26.             return false;  
  27.         }  
  28.     }  
  29.       
  30.     public int hashCode(){  
  31.         return this.name.hashCode() * this.age; //定义一个公式  
  32.     }  
  33.       
  34.     public static void main(String[] args){  
  35.         Set<Person> allSet = new HashSet<Person>();  
  36.         allSet.add(new Person("张三"30));  
  37.         allSet.add(new Person("李四"31));  
  38.         allSet.add(new Person("王五"32));  
  39.         allSet.add(new Person("王五"32));  
  40.         allSet.add(new Person("王五"32));  
  41.         allSet.add(new Person("赵六"33));  
  42.         allSet.add(new Person("孙七"33));  
  43.         System.out.println(allSet);  
  44.     }  
  45. }  
总结:

1.一个好的类应该覆写Object类中的equals()、hashCode()、toString()方法,String类中已经覆写完成了。
2.Set接口依靠hashCode()和equals()完成重复元素的判断

3.TreeSet依靠Comparable接口完成排序的。

0 0