不重复,无索引,可排序
可排序:按照元素的大小默认升序(由小到大)排序
TreeSet集合底层是基于红黑树的数据结构实现排序的,增删改查性能都比较好
注意:TreeSet集合是一定要排序的,可以将元素按照指定的规则进行排序
对于数值类型:Integer,Double,官方默认按照大小进行升序排序
- public class Test1 {
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- TreeSet<Integer> t=new TreeSet<>();
- t.add(88);
- t.add(22);
- t.add(11);
- t.add(99);
- t.add(33);
- System.out.println(t); //输出排序好的[11, 22, 33, 88, 99]
-
- }
-
- }
对于字符串类型:默认按照首字符的编号升序排序
- public class Test2 {
- public static void main(String[] args) {
- TreeSet<String> t=new TreeSet<>();
- t.add("AAA");
- t.add("BBB");
- t.add("www");
- t.add("aaa");
- t.add("vvv");
- t.add("牛马");
- t.add("ccc");
- System.out.println(t); //输出排序好的[AAA, BBB, aaa, ccc, vvv, www, 牛马]
- }
-
- }
对于自定义类型的Student对象,TreeSet无法直接排序。
如果想要使用TreeSet存储自定义类型,需要直接制定排序规则
创建一个学生类,先不重写它的比较规则
- package domeTreeset;
-
- public class Student {
- private String name;
- private int age;
- private double weight;
- @Override
- public String toString() {
- return "Student [name=" + name + ", age=" + age + ", weight=" + weight + "]";
- }
- /**
- * @return the name
- */
- public String getName() {
- return name;
- }
- /**
- * @param name the name to set
- */
- public void setName(String name) {
- this.name = name;
- }
- /**
- * @return the age
- */
- public int getAge() {
- return age;
- }
- /**
- * @param age the age to set
- */
- public void setAge(int age) {
- this.age = age;
- }
- /**
- * @return the weight
- */
- public double getWeight() {
- return weight;
- }
- /**
- * @param weight the weight to set
- */
- public void setWeight(double weight) {
- this.weight = weight;
- }
- public Student(String name, int age, double weight) {
- super();
- this.name = name;
- this.age = age;
- this.weight = weight;
- }
-
-
- }
测试一下输出程序直接就死了
- public class Test3 {
-
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Set<Student> s=new TreeSet();
- //添加4个学生对象
- s.add(new Student("张三", 20, 44.4));
- s.add(new Student("李四", 19, 55.3));
- s.add(new Student("王五", 14, 22.3));
- s.add(new Student("赵六", 30, 66.6));
- System.out.println(s); //直接输出程序就死了
-
-
-
- }
-
- }
两种方式中,返回值的规则
如果认为第一个元素大于第二个元素返回正整数即可
如果认为第一个元素小于第二个元素返回负整数即可
如果认为第一个元素等于第二个元素返回0即可,此时TreeSet集合只会保留一个元素,认为两者重复
方式1
让自定义的类(如学生类)实现Comparable接口,重写里面的compareTo方法来定制比较规则
实现Comparable后重写这个方法即可
- @Override
- public int compareTo(Student o) {
- // TODO Auto-generated method stub
- //return this.age-o.age;//按照年龄升序排序,如果年龄相等就去掉相同的
- return this.age-o.age>=0?1:-1; //按照年龄升序排序,不会去掉重复的
-
- }
方式2(建议优先使用)
TreeSet集合有参构造器,可以设置Compartor接口对应的比较器对象,来定制比较规则
- public class Test3 {
-
- public static <T> void main(String[] args) {
- // TODO Auto-generated method stub
- Set<Student> s=new TreeSet(new Comparator<Student>() {
-
- @Override
- public int compare(Student o1, Student o2) {
- // TODO Auto-generated method stub
- //return o1.getAge()-o2.getAge()>=0?1:-1;//按照年龄升序排序,年龄一样不删除
- return o1.getAge()-o2.getAge();//按照年龄升序排序,年龄一样就删除
- //return Double.compare(o1.getWeight(), o2.getWeight());//按照年龄升序排序
- }
- });
-
-
-
- //添加4个学生对象
- s.add(new Student("张三", 20, 44.4));
- s.add(new Student("李四", 19, 55.3));
- s.add(new Student("王五", 14, 22.3));
- s.add(new Student("赵六", 30, 66.6));
- s.add(new Student("牛马", 30, 66.6));
- System.out.println(s);
-
-
- }
-
- }
在用浮点型比较的时候,需要使用Double.compare()
如果两个规则定义的不一样,优先使用,本类的规则