• 重学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. }
  • 相关阅读:
    微信授权登录 | 全过程讲解[官方文档->代码梳理->注意点] uniapp+springboot(附Gitee源码)
    机器人操作系统ROS2学习—控制小海龟运动
    剑指Offer 09.用两个栈实现队列
    量子信息处理器可能能够提供高度压缩的生成对抗学习任务的版本
    Js基础:JS中怎么将数据转为布尔值
    Idea创建springboot工程的时候,发现pom文件没有带<parent>标签
    Spring Boot 统一处理功能
    Linux命令大全
    (ACM模式时)C++的输入输出需要注意的点
    SDP最佳实践丨为汽车品牌 L 铸造「数字化营销+管控」
  • 原文地址:https://blog.csdn.net/m0_73983707/article/details/139422497