The use of TreeSet of Java basics

TreeSet is a binary tree collection with unique elements, and TreeSet is an implementation class of the Set interface (TreeSet implements the Set interface), which has the characteristics of Set.

The characteristics of Set are: non-repeatable, elements are out of order, and there is no method with index (so ordinary for loops cannot be used to traverse, nor can the index be used to obtain or delete element values ​​in the Set collection).

Features of TreeSet: non-repeatable, no method with index (so ordinary for loop cannot be used to traverse, nor can index be used to obtain or delete element values ​​in TreeSet collection), elements can be sorted according to custom rules.

Next, we will learn TreeSet from the three characteristics of TreeSet and combine the code. (Because TreeSet does not have an index method, the second feature of TreeSet can only use forEach statement and Iterator iterator to realize TreeSet traversal)

注意:TreeSet的默认数据类型是Object类,理论上我们可以通过add()方法来添加任意数据类型,但是如果我们这样子做,当我们进行遍历输出的时候就会遇到麻烦!!!因此我们在使用TreeSet的时候,请使用泛型来约束数据类型!!!

1. Non-repeatable

The data type is a TreeSet of String type, and the output of TreeSet elements is realized by using ordinary output statements

public class Demo01 {
    
    
    public static void main(String[] args) {
    
    
        //若要使用TreeSet,我们需要导入import java.util.TreeSet;
        //我们使用泛型来约束TreeSet的数据类型为String类型
        TreeSet<String> set=new TreeSet<>();
        //使用add()方法来添加数据
        set.add("aa");
        set.add("aa");
        set.add("bb");
        //打印
        System.out.println(set);
    }
}

Running results (duplicate aa elements are filtered out)
insert image description here


TreeSet of Integer type, use ordinary output statement to realize the output of TreeSet elements

public class Demo01 {
    
    
    public static void main(String[] args) {
    
    
        //若要使用TreeSet,我们需要导入import java.util.TreeSet;
        //我们使用泛型来约束TreeSet的数据类型为Integer数据类型
        TreeSet<Integer> set=new TreeSet<>();
        //使用add()方法来添加数据
        set.add(18);
        set.add(20);
        set.add(20);
        //打印
        System.out.println(set);
    }
}

Running results (duplicate 20 elements are screened out, TreeSet is non-repeatable)
insert image description here


TreeSet operates custom Student type data

Student class

//若要在TreeSet中遍历我们自定义的数据类,我们需要复写Comparable接口中的compareTo()方法
public class Student implements Comparable<Student>{
    
    
    private String name;
    private char gender;
    private int age;
    private double salary;
    //无参构造方法
    public Student() {
    
    }
    //全参构造方法
    public Student(String name, char gender, int age, double salary) {
    
    
        this.name = name;
        this.gender = gender;
        this.age = age;
        this.salary = salary;
    }
    //Set,Get方法
    public String getName() {
    
    
        return name;
    }
    public void setName(String name) {
    
    
        this.name = name;
    }
    public char getGender() {
    
    
        return gender;
    }
    public void setGender(char gender) {
    
    
        this.gender = gender;
    }
    public int getAge() {
    
    
        return age;
    }
    public void setAge(int age) {
    
    
        this.age = age;
    }
    public double getSalary() {
    
    
        return salary;
    }
    public void setSalary(double salary) {
    
    
        this.salary = salary;
    }
    //toString方法
    @Override
    public String toString() {
    
    
        return "Student{" +
                "name='" + name + '\'' +
                ", gender=" + gender +
                ", age=" + age +
                ", salary=" + salary +
                '}';
    }
	//复写compareTo方法
    @Override
    public int compareTo(Student o) {
    
    
        return -1;
        //return的值不同,输出的结果就不同
        //若返回的结果是0,则只会返回第一个元素
        //若返回的结果是1,则会返回所有元素(按输入的顺序输出)
        //若返回的结果是-1,则会返回所有元素(按输入的顺序反向输出)
    }
}

The TreeSet of the Student type uses the forEach statement to realize the traversal output of the TreeSet elements

public class Demo01 {
    
    
    public static void main(String[] args) {
    
    
        //若要使用TreeSet,我们需要导入import java.util.TreeSet;
        //我们使用泛型来约束TreeSet的数据类型为Student类型
        TreeSet<Student> set=new TreeSet<>();
        //使用add()方法来添加数据
        set.add(new Student("zhangsan",'男',18,2500.00));
        set.add(new Student("lisi",'女',19,2600.00));
        set.add(new Student("wangwu",'男',20,3000.00));
        set.add(new Student("wangwu",'男',20,3000.00));
        //在此我们使用forEach来遍历TreeSet
        //Student是set的数据类型,我们将set的数据存入student(此名字你可以自定义)中并输出
        for(Student student:set){
    
    
            System.out.println(student);
        }
    }
}

Output of Student class return 0
insert image description here


The output of the Student class return 1
我们发现这里输出的内容含有重复的值,其实这并没有违反TreeSet不可重复这一条件,这是因为我们实际上并没有真正重写Comparable接口中的compareTo()方法,在元素进行比较的时候,计算机并没有通过比较他们的值来判定他们是否不同,而是通过比较他们存储的内存地址来比较他们是否不同,而我们都知道在我们存储数据时,都会为元素开辟一个内存空间,而开辟的内存空间各个内存空间的地址肯定是不同的,因此计算机认为他们是不同的。
insert image description here


Output of Student class return -1
我们发现这里输出的内容含有重复的值,其实这并没有违反TreeSet不可重复这一条件,这是因为我们实际上并没有真正重写Comparable接口中的compareTo()方法,在元素进行比较的时候,计算机并没有通过比较他们的值来判定他们是否不同,而是通过比较他们存储的内存地址来比较他们是否不同,而我们都知道在我们存储数据时,都会为元素开辟一个内存空间,而开辟的内存空间各个内存空间的地址肯定是不同的,因此计算机认为他们是不同的。
insert image description here


So how do we make the value of the data stored in the TreeSet non-repeatable? Let's rewrite the compareTo() method in the Comparable interface

2. Elements can be sorted according to our custom rules

The Student class rewrites the compareTo() method (using age sorting, the same age will not be stored, here we stipulate that the data is sorted by age, which is equivalent to telling the computer that even if the data names are different, they are considered the same if they are the same age)

//复写compareTo方法,按年龄进行排序
    @Override
    public int compareTo(Student o) {
    
    
        //这里我们根据年龄来排序,当我们存入一个数据后,它就变成了this,之后我们再存入数据,此数据就是o
        //例如我们刚开始存入(zhangsan,18),那么此时this.age=18,之后我们存入(lisi,19),那么o.getAge()=19
        //所以此时result=18-19=-1<0,表明传入对象(o)年龄>this对象年龄,传入对象存入右边即(zhangsan,18),(lisi,19)
        //之后我们又传入(wangwu,20),那么它会先于(zhangsan,18)比较,再与(lisi,19)比较,最后形成(zhangsan,18),(lisi,19),(wangwu,20)
        //最后我们传入(chenliu,20),那么它会先于(zhangsan,18)比较,再与(lisi,19)比较,最后再与(wangwu,20)比较,result=18-18=0,年龄相同,计算机认为它们是重复的,不在存入
        int result=this.age-o.getAge();
        return result;
    }

The TreeSet of the Student type uses the forEach statement to realize the traversal output of the TreeSet elements

public class Demo01 {
    
    
    public static void main(String[] args) {
    
    
        //若要使用TreeSet,我们需要导入import java.util.TreeSet;
        //我们使用泛型来约束TreeSet的数据类型为Student类型
        TreeSet<Student> set=new TreeSet<>();
        //使用add()方法来添加数据
        set.add(new Student("zhangsan",'男',18,2500.00));
        set.add(new Student("lisi",'女',19,2600.00));
        set.add(new Student("wangwu",'男',20,3000.00));
        set.add(new Student("chenliu",'男',20,3000.00));
        //在此我们使用forEach来遍历TreeSet
        //Student是set的数据类型,我们将set的数据存入student(此名字你可以自定义)中并输出
        for(Student student:set){
    
    
            System.out.println(student);
        }
    }
}

Running results: (You can see that wangwu and chenliu have different names, but they are the same age, and the computer will think they are duplicates)
insert image description here


The Student class rewrites the compareTo() method (use age and name to sort, the same age, rank name, if the name and age are the same, the computer will consider the data to be duplicated)

//复写compareTo方法
    @Override
    public int compareTo(Student o) {
    
    
        //这里我们先根据年龄来排序,当我们存入一个数据后,它就变成了this,之后我们再存入数据,此数据就是o
        //例如我们刚开始存入(zhangsan,18),那么此时this.age=18,之后我们存入(lisi,19),那么o.getAge()=19
        //所以此时result=18-19=-1<0,表明传入数据>当前数据,传入元素存入右边即(zhangsan,18),(lisi,19)
        //之后我们又传入(wangwu,20),那么它会先于(zhangsan,18)比较,再与(lisi,19)比较,最后形成(zhangsan,18),(lisi,19),(wangwu,20)
        //最后我们传入(chenliu,20),那么它会先于(zhangsan,18)比较,再与(lisi,19)比较,最后再与(wangwu,20)比较,年龄相同,对名字进行排序
        int result=this.age-o.getAge();
        
        //下面的语句指出,如果result==0(即年龄相同),则执行this.name.compareTo(o.getName())对名字进行排序,否则(年龄不同)执行result(按年龄排序),并将最终结果存储在变量中。
        //那么这样wangwu与chenliu的年龄相同时,会比较他们的姓名,名字不同,因此计算机会将它们视为不同对象,并根据它们的姓名进行排序
        result = result==0 ? this.name.compareTo(o.getName()) :result;
        return result;
    }

The TreeSet of the Student type uses the forEach statement to realize the traversal output of the TreeSet elements

public class Demo01 {
    
    
    public static void main(String[] args) {
    
    
        //若要使用TreeSet,我们需要导入import java.util.TreeSet;
        //我们使用泛型来约束TreeSet的数据类型为Student类型
        TreeSet<Student> set=new TreeSet<>();
        //使用add()方法来添加数据
        set.add(new Student("zhangsan",'男',18,2500.00));
        set.add(new Student("lisi",'女',19,2600.00));
        set.add(new Student("wangwu",'男',20,3000.00));
        set.add(new Student("chenliu",'男',20,3000.00));
        //在此我们使用forEach来遍历TreeSet
        //Student是set的数据类型,我们将set的数据存入student(此名字你可以自定义)中并输出
        for(Student student:set){
    
    
            System.out.println(student);
        }
    }
}

Running results: (You can see that both wangwu and chenliu output)
insert image description here


3. Use Iterator iterator to implement TreeSet element traversal output

public class Demo01 {
    
    
    public static void main(String[] args) {
    
    
        //若要使用TreeSet,我们需要导入import java.util.TreeSet;
        //我们使用泛型来约束TreeSet的数据类型为String类型
        TreeSet<String> set=new TreeSet<>();
        //使用add()方法来添加数据
        set.add("aa");
        set.add("aa");
        set.add("bb");
        //在此我们使用Iterator迭代器来遍历TreeSet
        //创建迭代器对象
        Iterator<String> iterator=set.iterator();
        //判断该位置是否有值,iterator.hasNext()它会查看我们当前位置是否存在元素,存在元素返回true,不存在元素返回false
        while(iterator.hasNext()){
    
    
        	//获取该位置的元素值,iterator.next()它会取得我们目前位置的元素,然后指针后移
            System.out.println(iterator.next());
        }
    }
}

Running result: (not repeatable)
insert image description here


OK! The application of these three characteristics of TreeSet is finished!

Guess you like

Origin blog.csdn.net/qq_45344586/article/details/129680447