• 重学java 58.红黑树相关集合


    现在还来得及

                           —— 24.6.3

    一、TreeSet

    1.概述:

            Treeset是set的实现类

    2.特点:

            a.对元素进行排序
            b.无索引
            c.不能存null
            d.线程不安全
            e.元素唯一

    3.数据结构:

            红黑树

    4.构造

            Treeset() —> 构造一个新的空 set,该 set 根据其元素的自然顺序进行排序 -> ASCII
            Treeset(comparator comparator) —> 构造一个新的空 Treeset,它根据指定比较器进行排序

    1. public class Person {
    2. private String name;
    3. private Integer age;
    4. public Person() {
    5. }
    6. public Person(String name, Integer age) {
    7. this.name = name;
    8. this.age = age;
    9. }
    10. public String getName() {
    11. return name;
    12. }
    13. public void setName(String name) {
    14. this.name = name;
    15. }
    16. public Integer getAge() {
    17. return age;
    18. }
    19. public void setAge(Integer age) {
    20. this.age = age;
    21. }
    22. @Override
    23. public String toString() {
    24. return "Person{" +
    25. "name='" + name + '\'' +
    26. ", age=" + age +
    27. '}';
    28. }
    29. }
    1. import java.util.TreeSet;
    2. public class Demo252TreeSet01 {
    3. public static void main(String[] args) {
    4. TreeSet set1 = new TreeSet<>();
    5. set1.add("c.白毛浮绿水");
    6. set1.add("a.鹅鹅鹅");
    7. set1.add("b.曲项向天歌");
    8. set1.add("d.红掌拨清波");
    9. System.out.println(set1); // [a.鹅鹅鹅, b.曲项向天歌, c.白毛浮绿水, d.红掌拨清波]
    10. }
    11. }
    1. import java.util.Comparator;
    2. import java.util.TreeSet;
    3. public class Demo253TreeSet02 {
    4. public static void main(String[] args) {
    5. TreeSet treeSet = new TreeSet<>(new Comparator() {
    6. @Override
    7. public int compare(Person o1, Person o2) {
    8. return o1.getAge()-o2.getAge();
    9. }
    10. });
    11. treeSet.add(new Person("小明",17));
    12. treeSet.add(new Person("小红",18));
    13. treeSet.add(new Person("小刚",16));
    14. System.out.println(treeSet); // [Person{name='小刚', age=16}, Person{name='小明', age=17}, Person{name='小红', age=18}]
    15. }
    16. }

    二、TreeMap

    1.概述:

            Treeset是set的实现类

    2.特点:

            a.对元素进行排序
            b.无索引
            c.key唯一
            d.线程不安全
            e.不能存null

    3.数据结构:

            红黑树

    4.构造:

            TreeMap() —> 使用键的自然顺序构造一个新的、空的树映射 —> ASCII
            TreeMap(Comparator comparator) —> 构造一个新的、空的树映射,该映射根据给定比较器进行排序

    1. import java.util.Comparator;
    2. import java.util.TreeMap;
    3. public class Demo254TreeMap01 {
    4. public static void main(String[] args) {
    5. TreeMap map = new TreeMap<>();
    6. map.put("a", "苦难是生活的花开");
    7. map.put("c","一切都会好的");
    8. map.put("b","我一直相信");
    9. System.out.println(map);
    10. TreeMap map2 = new TreeMap<>(new Comparator() {
    11. @Override
    12. public int compare(Person o1, Person o2) {
    13. return o1.getAge()-o2.getAge();
    14. }
    15. });
    16. map2.put(new Person("小明",17),"北京");
    17. map2.put(new Person("小红",18),"上海");
    18. map2.put(new Person("小刚",16),"江苏");
    19. System.out.println(map2);
    20. }
    21. }
  • 相关阅读:
    开了抖音小店,如何做抖店运营?实操经验分享!
    专访邦盛科技CEO王新宇:实时智能决策驱动“热数据” 价值绽放 | 爱分析访谈
    iOS开发Swift-7-得分,问题序号,约束对象,提示框,类方法与静态方法-趣味问答App
    八大排序算法-直接插入排序、希尔排序、直接选择排序、冒泡排序、堆排序、快速排序、归并排序、基数排序(下)
    ubuntu编写makefile编译c++程序
    图解Bellman Ford算法
    基于黏菌优化的BP神经网络(分类应用) - 附代码
    UI设计工具都哪些常用的,推荐这5款
    CleanMyMac X2024告别硬盘空间不足,让您的Mac电脑极速如新
    正则表达式规则以及贪婪匹配与非贪婪匹配详解
  • 原文地址:https://blog.csdn.net/m0_73983707/article/details/139422497