treeSet的顺序
来源:互联网 发布:mac其他文件怎么清理 编辑:程序博客网 时间:2024/06/06 03:43
- import java.util.Set;
- import java.util.TreeSet;
-
- public class Person{
- private String name;
- private int age;
- public Person(String name, int age){
- this.name = name;
- this.age = age;
- }
- public String toString(){
- return "姓名:" + this.name + ";年龄:" + this.age;
- }
-
- public static void main(String[] args){
- Set<Person> allSet = new TreeSet<Person>();
- allSet.add(new Person("张三", 30));
- allSet.add(new Person("李四", 31));
- allSet.add(new Person("王五", 32));
- allSet.add(new Person("王五", 32));
- allSet.add(new Person("王五", 33));
- allSet.add(new Person("赵六", 33));
- allSet.add(new Person("孙七", 33));
- System.out.println(allSet);
- }
- }
执行时出现以下错误:
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()方法
- import java.util.Set;
- import java.util.TreeSet;
-
- public class Person implements Comparable<Person>{
- private String name;
- private int age;
- public Person(String name, int age){
- this.name = name;
- this.age = age;
- }
- public String toString(){
- return "姓名:" + this.name + ";年龄:" + this.age;
- }
-
- public int compareTo(Person per){
- if(this.age < per.age){
- return 1;
- }else if(this.age > per.age){
- return -1;
- }else{
- return 0;
- }
- }
-
- public static void main(String[] args){
- Set<Person> allSet = new TreeSet<Person>();
- allSet.add(new Person("张三", 30));
- allSet.add(new Person("李四", 31));
- allSet.add(new Person("王五", 32));
- allSet.add(new Person("王五", 32));
- allSet.add(new Person("王五", 33));
- allSet.add(new Person("赵六", 33));
- allSet.add(new Person("孙七", 33));
- System.out.println(allSet);
- }
- }
此时完成了排序,但是年龄相同的元素没有了,继续修改。- import java.util.Set;
- import java.util.TreeSet;
-
- public class Person implements Comparable<Person>{
- private String name;
- private int age;
- public Person(String name, int age){
- this.name = name;
- this.age = age;
- }
- public String toString(){
- return "姓名:" + this.name + ";年龄:" + this.age;
- }
-
- public int compareTo(Person per){
- if(this.age < per.age){
- return 1;
- }else if(this.age > per.age){
- return -1;
- }else{
- return this.name.compareTo(per.name);
- }
- }
-
- public static void main(String[] args){
- Set<Person> allSet = new TreeSet<Person>();
- allSet.add(new Person("张三", 30));
- allSet.add(new Person("李四", 31));
- allSet.add(new Person("王五", 32));
- allSet.add(new Person("王五", 32));
- allSet.add(new Person("王五", 33));
- allSet.add(new Person("赵六", 33));
- allSet.add(new Person("孙七", 33));
- System.out.println(allSet);
- }
- }
此时并没有去掉重复的元素,要想去掉重复元素,则需要Object类中的两个方法帮助:1.hashCode():表示一个唯一的编码,一般通过计算表示
2.equals():进行对象的比较操作
- import java.util.Set;
- import java.util.HashSet;
-
- public class Person{
- private String name;
- private int age;
- public Person(String name, int age){
- this.name = name;
- this.age = age;
- }
- public String toString(){
- return "姓名:" + this.name + ";年龄:" + this.age;
- }
-
- public boolean equals(Object obj){
- if(this == obj){
- return true;
- }
- if(!(obj instanceof Person)){
- return false;
- }
- Person p = (Person)obj;
- if(this.name.equals(p.name) && this.age == p.age){
- return true;
- }else{
- return false;
- }
- }
-
- public int hashCode(){
- return this.name.hashCode() * this.age;
- }
-
- public static void main(String[] args){
- Set<Person> allSet = new HashSet<Person>();
- allSet.add(new Person("张三", 30));
- allSet.add(new Person("李四", 31));
- allSet.add(new Person("王五", 32));
- allSet.add(new Person("王五", 32));
- allSet.add(new Person("王五", 32));
- allSet.add(new Person("赵六", 33));
- allSet.add(new Person("孙七", 33));
- System.out.println(allSet);
- }
- }
总结:1.一个好的类应该覆写Object类中的equals()、hashCode()、toString()方法,String类中已经覆写完成了。
2.Set接口依靠hashCode()和equals()完成重复元素的判断
3.TreeSet依靠Comparable接口完成排序的。
0 0