TreeSet是Set集合的常见子类.
TreeSet:底层结构是 二叉树
元素是有排序的,但是不可以有重复元素.
相关代码演练:
/*TreeSet ;元素是有序的,但是不可以元素重复. */import java.util.*;class TreeSetDemo{ public static void main(String [] args) { TreeSet ts = new TreeSet(); ts.add("java01"); ts.add("java03"); ts.add("java02"); ts.add("java04"); ts.add("java04"); Iterator it = ts.iterator(); while(it.hasNext()) { sop(it.next()); } ts.remove("java02"); sop(ts); } public static void sop(Object obj) { System.out.println(obj); } }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; }}
TreeSet的两种排序方式:
第一种排序方式:(让元素自身具备比较性)让该类实现Comparable接口.并且覆写compareTo方法.这种排序是自然排序.
保证元素唯一性的依据是: compareTo方法return 0;
代码演示:
/* TreeSet存储自定义对象 (会抛出类型转换异常ClassCastException) Student类无法转成Comparable. 因此Student类需要实现Comparable接口.并且覆写compareTo方法.排序的方式为自然顺序. 排序时,当主要条件相同时,还要判断一下次要条件. 需求: 按学生的年龄排序.*/import java.util.*;class TreeSetTest{ public static void main(String [] args) { TreeSet ts = new TreeSet(); ts.add(new Student("lisi08",19)); ts.add(new Student("lisi02",22)); ts.add(new Student("lisi007",20)); ts.add(new Student("lisi09",19)); 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 Student implements Comparable //该接口强制让学生具备了比较性. (实现了该接口需要覆盖其compareTo方法){ private String name; private int age; Student(String name,int age) { this.name = name; this.age = age; } public int compareTo(Object obj) { //return 1; //怎么存的怎么取出 //return -1; //倒取存入的对象 //return 0; //只会存入第一个存入的对象. if(!(obj instanceof Student)) throw new RuntimeException("不是学生对象"); Student s = (Student)obj; System.out.println(this.name+"---compareTo---"+s.name); 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; }}
第二种排序方式:(元素不具备比较性,或者比较性是所不需要的)此时,让集合自身具备比较性.在集合一初始化时,就有比较方式.
实现方式: 定义一个类,实现comparator接口,并覆写其中的compare方法.
/*让容器自身具备比较性,定义一个比较器.将比较器对象传递给TreeSet集合的构造函数;思路步骤:定义一个容器让这个容器实现comparator,并且覆写其中的compare方法.*/import java.util.*;class TreeSetTest2{ public static void main(String [] args) { //TreeSet ts = new TreeSet(); //传入的不是比较器对象时,调用的是CompareTo方法. TreeSet ts = new TreeSet(new MyComparator()); //将比较器传递给TreeSet的构造函数. ts.add(new Student("lisi08",19)); ts.add(new Student("lisi02",22)); ts.add(new Student("lisi02",23)); ts.add(new Student("lisi007",20)); ts.add(new Student("lisi09",19)); 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 Student implements Comparable //让学生类具备了比较性{ 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; } public int compareTo(Object obj) //覆写了Comparable中的compareTo方法. { if(!(obj instanceof Student)) throw new RuntimeException("bushixueshengduixiang"); Student s = (Student)obj; if(this.age>s.age) return 1; if(this.age==s.age) { return this.name.compareTo(s.name); } return -1; }}class MyComparator implements Comparator //定义一个容器并实现了Comparator接口{ public int compare(Object o1,Object o2) //覆写其中的compare方法. { if(!(o1 instanceof Student ||o2 instanceof Student)) throw new RuntimeException("对象错误."); Student s1 = (Student)o1; Student s2 = (Student)o2; //return s1.getName().compareTo(s2.getName()); //无法存储到姓名相同年龄不同的对象. int num = s1.getName().compareTo(s2.getName()); //判断返回值 (1 0 -1) if(num==0) //判断的名字相同时,在判断其年龄是否相同. { return s1.getAge()-s2.getAge(); /* if(s1.getAge()>s2.getAge()) return 1; if(s1.getAge()==s2.getAge()) return 0; return -1; */ } return num; }}
练习:
按照字符串的长度进行排序.
/*练习:按照字符串长度排序*/import java.util.*;class TreeSetExam{ public static void main(String [] args) { TreeSet ts = new TreeSet(new StrLengthCompare()); ts.add("abcd"); ts.add("cc"); ts.add("cba"); ts.add("a"); ts.add("hahaha"); ts.add("aa"); Iterator it = ts.iterator(); while(it.hasNext()) { sop(it.next()); } } public static void sop(Object obj) { System.out.println(obj); } }class StrLengthCompare implements Comparator{ public int compare(Object o1,Object o2) { if(!(o1 instanceof String || o2 instanceof String)) throw new RuntimeException("ERROR"); String s1 = (String)o1; String s2 = (String)o2; //int num = s1.length()-s2.length(); int num = new Integer(s1.length()).compareTo(new Integer(s2.length())); if(num==0) //判断长度一样的字符串的自然排序. { return s1.compareTo(s2); } return num; }}
注意:一定要记得先对主要条件进行判断,在对次要条件进行判断.避免出现漏存的情况.