TreeMap

TreeMap

TreeMap底层是红黑树

根据创建时调用的构造方法不同,map中的键排序的规则不同

创建TreeMap是无参构造方法的话,将来map中的键是以自然排序

创建TreeMap是有参构造方法,传入Comparator接口的实现类对象(匿名内部类的方式)

package com.shujia.day14;
import java.util.TreeMap;

public class TreeMapDemo1 {
    public static void main(String[] args) {
        // 创建TreeMap由无参构造方法
        TreeMap<Integer, String> map1 = new TreeMap<>();

        map1.put(1004,"张三");
        map1.put(1005,"李四");
        map1.put(1002,"王五");
        map1.put(1001,"赵六");
        map1.put(1004,"王二麻");
        System.out.println(map1);
    }
}

结果是按照key进行升序的

image-20240308205810307

将学生类型作为键的类型,按照年龄从小到大排序,当学生的姓名和年龄一样的时候,要进行去重

package com.shujia.day14;
import java.util.TreeMap;

public class TreeMapDemo2 {
    public static void main(String[] args) {
        TreeMap<Student1, String> map1 = new TreeMap<>();

        map1.put(new Student1("张三",18),"打游戏");
        map1.put(new Student1("张三2",17),"打游戏2");
        map1.put(new Student1("张三3",13),"打游戏3");
        map1.put(new Student1("张三4",17),"打游戏4");
        map1.put(new Student1("张三5",19),"打游戏5");
        map1.put(new Student1("张三",18),"打游戏6");
        System.out.println(map1);
    }
}

报错原因:这里是无参构造方法创建的对象,而之前学的无参构造方法构造的Comparator的值是null,在调用put方法运行赋值给cpr的时候,cpr的值是null,运行的时候是自然排序,且不运行if代码,运行的是else自然排序的代码,键值对的键需要转换成Comparable类型,而现在的键的类型是Student1类型的,Student1转不成Comparable类型(因为没有实现该类型),所以实现以下Comparable类型并且重写ComparTo方法

image-20240308210658777

实现并重写之后:(主要是对Student1类进行改写)

package com.shujia.day14;
import java.util.Objects;

public class Student1 implements Comparable<Student1>{
        private String name;
        private int age;

        public Student1() {
        }

        public Student1(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Student1 student1 = (Student1) o;
            return age == student1.age && Objects.equals(name, student1.name);
        }

        @Override
        public int hashCode() {
            return Objects.hash(name, age);
        }

        @Override
        public String toString() {
            return "Student1{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }

    @Override
    public int compareTo(Student1 o) {
        //显式条件:按照年龄从小到大排序
        int i1 = this.getAge() - o.getAge();
        //隐藏条件:年龄一样的时候,姓名不一定一样
        return (i1 == 0) ? this.getName().compareTo(o.getName()) : i1;
    }
}

源代码不用改

package com.shujia.day14;
import java.util.TreeMap;

public class TreeMapDemo2 {
    public static void main(String[] args) {
        TreeMap<Student1, String> map1 = new TreeMap<>();

        map1.put(new Student1("张三",18),"打游戏");
        map1.put(new Student1("张三2",17),"打游戏2");
        map1.put(new Student1("张三3",13),"打游戏3");
        map1.put(new Student1("张三4",17),"打游戏4");
        map1.put(new Student1("张三5",19),"打游戏5");
        map1.put(new Student1("张三",18),"打游戏6");
        System.out.println(map1);
    }
}

image-20240308212049455

posted @ 2024-03-08 21:24  peculiar-  阅读(2)  评论(0编辑  收藏  举报