TreeSet:可以对Set集合中的元素进行排序。
例:存放自定义对象
import java.util.*; /* 需求:往TreeSet集合中存储自定义对象--学生 想按照学生的年龄进行排序 */ class Student{ private String name; private int age; Student(String name,int age){ this.name = name; this.age = age; } public String getName(){ return name; } public int getAge(){ return age; } } class TreeSetDemo{ public static void main(String[] args){ TreeSet ts = new TreeSet(); ts.add(new Student("lisi02",22)); ts.add(new Student("lisi007",20)); ts.add(new Student("lisi09",19)); ts.add(new Student("lisi01",40)); Iterator it = ts.iterator(); while(it.hasNext()){ Student stu = (Student)it.next(); sop(stu.getName()+"::"+stu.getAge()); } } public static void sop(Object obj){ System.out.println(obj); } }
然而输出结果是:
这是因为TreeSet会自动排序,然而我们存放的自定义对象没有比较性,所以报错。
解决方法:让Student类实现Comparable接口,强制让学生具备比较性
import java.util.*; /* 需求:往TreeSet集合中存储自定义对象--学生 想按照学生的年龄进行排序 */ class Student implements Comparable{ private String name; private int age; Student(String name,int age){ this.name = name; this.age = age; } public int compareTo(Object obj){ if(!(obj instanceof Student)) throw new RuntimeException("不是学生对象"); Student s = (Student)obj; return this.age-s.age; } public String getName(){ return name; } public int getAge(){ return age; } } class TreeSetDemo{ public static void main(String[] args){ TreeSet ts = new TreeSet(); ts.add(new Student("lisi02",22)); ts.add(new Student("lisi007",20)); ts.add(new Student("lisi09",19)); ts.add(new Student("lisi01",40)); Iterator it = ts.iterator(); while(it.hasNext()){ Student stu = (Student)it.next(); sop(stu.getName()+"::"+stu.getAge()); } } public static void sop(Object obj){ System.out.println(obj); } }
如果年龄相同,姓名不同,会出现什么情况呢?
import java.util.*; /* 需求:往TreeSet集合中存储自定义对象--学生 想按照学生的年龄进行排序 */ class Student implements Comparable{ private String name; private int age; Student(String name,int age){ this.name = name; this.age = age; } public int compareTo(Object obj){ if(!(obj instanceof Student)) throw new RuntimeException("不是学生对象"); Student s = (Student)obj; return this.age-s.age; } public String getName(){ return name; } public int getAge(){ return age; } } class TreeSetDemo{ public static void main(String[] args){ TreeSet ts = new TreeSet(); ts.add(new Student("lisi02",22)); ts.add(new Student("lisi007",20)); ts.add(new Student("lisi09",19)); ts.add(new Student("lisi01",40)); ts.add(new Student("lisi08",40)); Iterator it = ts.iterator(); while(it.hasNext()){ Student stu = (Student)it.next(); sop(stu.getName()+"::"+stu.getAge()); } } public static void sop(Object obj){ System.out.println(obj); } }
发现新元素没有存进来,这是因为根据比较规则,相同年龄即代表同一元素,根据不可重复性,该元素没有存进来。怎么解决?如下:
import java.util.*; /* 需求:往TreeSet集合中存储自定义对象--学生 想按照学生的年龄进行排序 */ class Student implements Comparable{ private String name; private int age; Student(String name,int age){ this.name = name; this.age = age; } public int compareTo(Object obj){ if(!(obj instanceof Student)) throw new RuntimeException("不是学生对象"); Student s = (Student)obj; if(this.age>s.age) return 1; if(this.age==s.age){ return this.name.compareTo(s.name); } return -1; } public String getName(){ return name; } public int getAge(){ return age; } } class TreeSetDemo{ public static void main(String[] args){ TreeSet ts = new TreeSet(); ts.add(new Student("lisi02",22)); ts.add(new Student("lisi007",20)); ts.add(new Student("lisi09",19)); ts.add(new Student("lisi01",40)); ts.add(new Student("lisi08",40)); Iterator it = ts.iterator(); while(it.hasNext()){ Student stu = (Student)it.next(); sop(stu.getName()+"::"+stu.getAge()); } } public static void sop(Object obj){ System.out.println(obj); } }
总结:排序时,当主要条件相同时,一定要判断次要条件
---------------------------------------------------------------------------------
TreeSet底层的数据结构为二叉树。保证元素唯一性的依据:compareTo方法return 0.
TreeSet排序的第一种方式:让元素自身具备比较性,元素需要实现Comparable接口,覆盖compareTo方法,这种方式也称为元素的自然顺序,或者叫做默认顺序
TreeSet集合的第二种排序方式:当元素自身不具备比较性时,或者具备的比较性不是所需要的,这时就需要让集合自身具备比较性。在集合初始化时,就有了比较方式
import java.util.*; /* 当元素自身不具备比较性,或者具备的比较性不是所需要的 这时需要让容器自身具备比较性。 定义了比较器,将比较器对象作为参数传递给TreeSet集合的构造函数 当两种排序都存在时,以比较器为主 定义一个类,实现Comparator接口,覆盖compare方法(注意:comparable里面才是compareTo方法) */ class Student implements Comparable{ private String name; private int age; Student(String name,int age){ this.name = name; this.age = age; } public int compareTo(Object obj){ if(!(obj instanceof Student)) throw new RuntimeException("不是学生对象"); Student s = (Student)obj; if(this.age>s.age) return 1; if(this.age==s.age){ return this.name.compareTo(s.name); } return -1; } public String getName(){ return name; } public int getAge(){ return age; } } class TreeSetDemo2{ public static void main(String[] args){ TreeSet ts = new TreeSet(); ts.add(new Student("lisi02",22)); ts.add(new Student("lisi007",20)); ts.add(new Student("lisi007",21)); ts.add(new Student("lisi09",19)); ts.add(new Student("lisi01",40)); ts.add(new Student("lisi08",40)); Iterator it = ts.iterator(); while(it.hasNext()){ Student stu = (Student)it.next(); sop(stu.getName()+"::"+stu.getAge()); } } public static void sop(Object obj){ System.out.println(obj); } } class MyCompare implements Comparator{ public int compare(Object o1,Object o2){ Student s1 = (Student)o1; Student s2 = (Student)o2; int num = s1.getName().compareTo(s2.getName()); if(num==0){ if(s1.getAge()>s2.getAge()) return 1; if(s1.getAge()==s2.getAge()) return 0; return -1;//更简单的写法:return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge())); } return num; } }
-----------------------------------------------------------------------
TreeSet练习:按照字符串长度排序
一开始的代码如下:
/* 字符串本身具备比较性,但是它的比较方式不是所需要的,这时就只能使用比较器 */ import java.util.*; class TreeSetTest{ public static void main(String[] args){ TreeSet ts =new TreeSet(); ts.add("abcd"); ts.add("cc"); ts.add("cba"); ts.add("z"); ts.add("hahaha"); Iterator it = ts.iterator(); while(it.hasNext()){ sop(it.next()); } } public static void sop(Object obj){ System.out.println(obj); } }
结果为
显然不是我们想要的。
/* 字符串本身具备比较性,但是它的比较方式不是所需要的,这时就只能使用比较器 */ import java.util.*; class TreeSetTest{ public static void main(String[] args){ TreeSet ts =new TreeSet(new StringLengthComparator()); ts.add("abcd"); ts.add("cc"); ts.add("cba"); ts.add("z"); ts.add("hahaha"); Iterator it = ts.iterator(); while(it.hasNext()){ sop(it.next()); } } public static void sop(Object obj){ System.out.println(obj); } } class StringLengthComparator implements Comparator{ public int compare(Object o1,Object o2){ String s1 = (String)o1; String s2 = (String)o2; return s1.length()-s2.length(); } }
可以优化的地方:当出现相同长度,不同内容的字符串时,会因为长度相同而被视为相同元素,不被存储进来,所以在实现比较器接口的类中应该判断当长度相同时,比较字符串内容。即:
class StringLengthComparator implements Comparator{ public int compare(Object o1,Object o2){ String s1 = (String)o1; String s2 = (String)o2; //return s1.length()-s2.length(); int num = new Integer(s1.length()).compareTo(new Integer(s2.length())); if(num==0) return s1.compareTo(s2); return num; } }
时间: 2024-10-18 19:26:27