• TreeSet集合概述和特点(有代码)


    特点:

    不重复,无索引,可排序

    可排序:按照元素的大小默认升序(由小到大)排序

    TreeSet集合底层是基于红黑树的数据结构实现排序的,增删改查性能都比较好

    注意:TreeSet集合是一定要排序的,可以将元素按照指定的规则进行排序

    TreeSet集合默认排序的规则

    对于数值类型:Integer,Double,官方默认按照大小进行升序排序

    1. public class Test1 {
    2. public static void main(String[] args) {
    3. // TODO Auto-generated method stub
    4. TreeSet<Integer> t=new TreeSet<>();
    5. t.add(88);
    6. t.add(22);
    7. t.add(11);
    8. t.add(99);
    9. t.add(33);
    10. System.out.println(t); //输出排序好的[11, 22, 33, 88, 99]
    11. }
    12. }

    对于字符串类型:默认按照首字符的编号升序排序

    1. public class Test2 {
    2. public static void main(String[] args) {
    3. TreeSet<String> t=new TreeSet<>();
    4. t.add("AAA");
    5. t.add("BBB");
    6. t.add("www");
    7. t.add("aaa");
    8. t.add("vvv");
    9. t.add("牛马");
    10. t.add("ccc");
    11. System.out.println(t); //输出排序好的[AAA, BBB, aaa, ccc, vvv, www, 牛马]
    12. }
    13. }

    对于自定义类型的Student对象,TreeSet无法直接排序。

    如果想要使用TreeSet存储自定义类型,需要直接制定排序规则

    创建一个学生类,先不重写它的比较规则

    1. package domeTreeset;
    2. public class Student {
    3. private String name;
    4. private int age;
    5. private double weight;
    6. @Override
    7. public String toString() {
    8. return "Student [name=" + name + ", age=" + age + ", weight=" + weight + "]";
    9. }
    10. /**
    11. * @return the name
    12. */
    13. public String getName() {
    14. return name;
    15. }
    16. /**
    17. * @param name the name to set
    18. */
    19. public void setName(String name) {
    20. this.name = name;
    21. }
    22. /**
    23. * @return the age
    24. */
    25. public int getAge() {
    26. return age;
    27. }
    28. /**
    29. * @param age the age to set
    30. */
    31. public void setAge(int age) {
    32. this.age = age;
    33. }
    34. /**
    35. * @return the weight
    36. */
    37. public double getWeight() {
    38. return weight;
    39. }
    40. /**
    41. * @param weight the weight to set
    42. */
    43. public void setWeight(double weight) {
    44. this.weight = weight;
    45. }
    46. public Student(String name, int age, double weight) {
    47. super();
    48. this.name = name;
    49. this.age = age;
    50. this.weight = weight;
    51. }
    52. }

    测试一下输出程序直接就死了

    1. public class Test3 {
    2. public static void main(String[] args) {
    3. // TODO Auto-generated method stub
    4. Set<Student> s=new TreeSet();
    5. //添加4个学生对象
    6. s.add(new Student("张三", 20, 44.4));
    7. s.add(new Student("李四", 19, 55.3));
    8. s.add(new Student("王五", 14, 22.3));
    9. s.add(new Student("赵六", 30, 66.6));
    10. System.out.println(s); //直接输出程序就死了
    11. }
    12. }

    自定以排序规则

    两种方式中,返回值的规则

    如果认为第一个元素大于第二个元素返回正整数即可

    如果认为第一个元素小于第二个元素返回负整数即可

    如果认为第一个元素等于第二个元素返回0即可,此时TreeSet集合只会保留一个元素,认为两者重复

    TreeSet集合存储对象的时候有两种方式可以设计自定义比较规则

    方式1

    让自定义的类(如学生类)实现Comparable接口,重写里面的compareTo方法来定制比较规则

    实现Comparable后重写这个方法即可

    1. @Override
    2. public int compareTo(Student o) {
    3. // TODO Auto-generated method stub
    4. //return this.age-o.age;//按照年龄升序排序,如果年龄相等就去掉相同的
    5. return this.age-o.age>=0?1:-1; //按照年龄升序排序,不会去掉重复的
    6. }

    方式2(建议优先使用)

    TreeSet集合有参构造器,可以设置Compartor接口对应的比较器对象,来定制比较规则

    1. public class Test3 {
    2. public static <T> void main(String[] args) {
    3. // TODO Auto-generated method stub
    4. Set<Student> s=new TreeSet(new Comparator<Student>() {
    5. @Override
    6. public int compare(Student o1, Student o2) {
    7. // TODO Auto-generated method stub
    8. //return o1.getAge()-o2.getAge()>=0?1:-1;//按照年龄升序排序,年龄一样不删除
    9. return o1.getAge()-o2.getAge();//按照年龄升序排序,年龄一样就删除
    10. //return Double.compare(o1.getWeight(), o2.getWeight());//按照年龄升序排序
    11. }
    12. });
    13. //添加4个学生对象
    14. s.add(new Student("张三", 20, 44.4));
    15. s.add(new Student("李四", 19, 55.3));
    16. s.add(new Student("王五", 14, 22.3));
    17. s.add(new Student("赵六", 30, 66.6));
    18. s.add(new Student("牛马", 30, 66.6));
    19. System.out.println(s);
    20. }
    21. }

    在用浮点型比较的时候,需要使用Double.compare()

    如果两个规则定义的不一样,优先使用,本类的规则

  • 相关阅读:
    普中51单片机学习(EEPROM)
    Linux网络编程系列之服务器编程——信号驱动模型
    怎样设计产品的兼容性测试更好?
    Redis-基本介绍/linux下环境配置/配置文件
    Unity fbx动画压缩自动化
    一篇文章让你没有爬不到的shipin,只有顶不住的shipin
    代码随想录阅读笔记-字符串【翻转字符串中单词】
    leetcode 1342.将数字变成0的操作次数
    SQL Server 日期范围按每月一行拆分
    【MindSpore易点通】常用定位方法之按Cell粒度Dump
  • 原文地址:https://blog.csdn.net/m0_64365315/article/details/125632107