TreeSet可以对Set集合中的元素进行排序
判断元素唯一性的方式:就是根据比较方法的返回结果是否为0,是0:就是相同元素,就不会存储
什么比较方法,先不管它,我们先按照以前学过的集合定义和使用
一、自定义元素类型
1 public class Person { 2 3 private String name; 4 private int age; 5 6 public Person() { 7 super(); 8 } 9 10 public Person(String name, int age) { 11 super(); 12 this.name = name; 13 this.age = age; 14 } 15 16 public String getName() { 17 return name; 18 } 19 20 public void setName(String name) { 21 this.name = name; 22 } 23 24 public int getAge() { 25 return age; 26 } 27 28 public void setAge(int age) { 29 this.age = age; 30 } 31 32 @Override 33 public int hashCode() { 34 System.out.println(this + ":-------hashCode"); 35 return this.name.hashCode() + this.age; 36 } 37 38 @Override 39 public boolean equals(Object obj) { 40 System.out.println(this + ":-------equals-------" + obj); 41 if (!(obj instanceof Person)) 42 throw new ClassCastException("类型不对"); 43 Person person = (Person) obj; 44 return this.name.equals(person.name) && person.age == this.age; 45 } 46 47 @Override 48 public String toString() { 49 50 return this.name + ":" + this.age; 51 } 52 }
二、业务逻辑代码
1 import java.util.Iterator; 2 import java.util.TreeSet; 3 4 import cn.marw.common.bean.Person; 5 6 public class TreeSetDemo { 7 8 public static void main(String[] args) { 9 TreeSet ts = new TreeSet(); 10 11 ts.add(new Person("zhangsan", 28)); 12 ts.add(new Person("lisi", 21)); 13 ts.add(new Person("wangwu", 23)); 14 ts.add(new Person("zhaoliu", 29)); 15 ts.add(new Person("zhouqi", 19)); 16 ts.add(new Person("sunba", 30)); 17 18 Iterator it = ts.iterator(); 19 20 while(it.hasNext()) { 21 Person p=(Person)it.next(); 22 System.out.println(p.getName()+":"+p.getAge()); 23 } 24 } 25 26 }
运行结果:
错误信息:Exception in thread "main" java.lang.ClassCastException: class cn.marw.common.bean.Person cannot be cast to class java.lang.Comparable 无法将Person转换为Comparable
首先想到就是修改Person类,这就是TreeSet集合排序的第一种方式(称之为:元素自然顺序):让元素类型具有比较性(实现Comparable接口,覆盖compareTo方法):
1 public class Person implements Comparable { 2 3 private String name; 4 private int age; 5 6 public Person() { 7 super(); 8 } 9 10 public Person(String name, int age) { 11 super(); 12 this.name = name; 13 this.age = age; 14 } 15 16 public String getName() { 17 return name; 18 } 19 20 public void setName(String name) { 21 this.name = name; 22 } 23 24 public int getAge() { 25 return age; 26 } 27 28 public void setAge(int age) { 29 this.age = age; 30 } 31 32 @Override 33 public int hashCode() { 34 System.out.println(this + ":-------hashCode"); 35 return this.name.hashCode() + this.age; 36 } 37 38 @Override 39 public boolean equals(Object obj) { 40 System.out.println(this + ":-------equals-------" + obj); 41 if (!(obj instanceof Person)) 42 throw new ClassCastException("类型不对"); 43 Person person = (Person) obj; 44 return this.name.equals(person.name) && person.age == this.age; 45 } 46 47 @Override 48 public String toString() { 49 50 return this.name + ":" + this.age; 51 } 52 53 @Override 54 public int compareTo(Object o) { 55 //定义Person类型的自然顺序为:根据年龄排序,年龄相同在根据名称排序 56 if(!(o instanceof Person)){ 57 throw new ClassCastException("类型错误"); 58 } 59 Person p=(Person)o; 60 int temp=this.age-p.age; 61 return temp==0?this.name.compareTo(p.name):temp; 62 } 63 }
结果这里没有做年龄相同,姓名不能的数据:
这种方式虽然解决了问题,我们想想如果我们修改排序方式那不就影响别人的程序了吗。
第二种方式就来了,让集合具有比较性
具体做法:
1、创建一个类实现Comparator接口,并重写compare方法
2、在实例化TreeSet集合时,把创建的类作为参数传递给TreeSet构造函数
实现Comparator接口的类代码:
1 import java.util.Comparator; 2 3 import cn.marw.common.bean.Person; 4 5 public class PersonComparatorByName implements Comparator { 6 7 @Override 8 public int compare(Object o1, Object o2) { 9 //只比较name 10 if(!(o1 instanceof Person)||!(o2 instanceof Person)) 11 throw new ClassCastException("类型错误"); 12 13 return ((Person)o1).getName().compareTo(((Person)o2).getName()); 14 } 15 16 }
实例化TreeSet代码
1 import java.util.Iterator; 2 import java.util.TreeSet; 3 4 import cn.marw.common.bean.Person; 5 6 public class TreeSetDemo { 7 8 public static void main(String[] args) { 9 TreeSet ts = new TreeSet(new PersonComparatorByName()); 10 11 ts.add(new Person("zhangsan", 28)); 12 ts.add(new Person("lisi", 21)); 13 ts.add(new Person("wangwu", 23)); 14 ts.add(new Person("zhaoliu", 29)); 15 ts.add(new Person("zhouqi", 19)); 16 ts.add(new Person("sunba", 30)); 17 18 Iterator it = ts.iterator(); 19 20 while(it.hasNext()) { 21 Person p=(Person)it.next(); 22 System.out.println(p.getName()+":"+p.getAge()); 23 } 24 } 25 }
结果: