Collection of nagging SE-08 - TreeSet

Keywords: Java

8. TreeSet

Use the natural order of the elements to sort them, or use the Comparator provided when creating the set.

The underlying data structure is the red black tree (the red black tree is a self balanced binary tree, characterized by large left and small right)

Knowledge of red black trees, stamp: Mangrove , or: The easiest to understand red black tree

 

We wrote a Person in the HashSet. Let's save it in the TreeSet:

import java.util.TreeSet;

public class Test {
    public static void main(String[] args) {
        TreeSet<Person> set = new TreeSet<>();
        set.add(new Person(1, "Spicy strips"));
        set.add(new Person(3, "Popsicle"));
        set.add(new Person(4, "bread"));
        set.add(new Person(2, "Potato chips"));
        set.add(new Person(2, "Potato chips"));
        set.add(new Person(2, "Potato chips"));
        for (Person person : set) {
            System.out.println(person);
        }
    }
}

class Person {
    public int id;
    public String name;
    public Person(int id, String name) {
        this.id = id;
        this.name = name;
    }
    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + "]";
    }
}

An error was reported after running...

be careful!!!

The TreeSet stored type must implement the compatible interface and override the compareTo method, otherwise ClassCastException will be thrown

 

Let's use the Person class to implement the compatible interface, and then store it in the TreeSet:

(note that only - 1, 0, 1 can be returned here)

import java.util.TreeSet;

public class Test {
    public static void main(String[] args) {
        TreeSet<Person> set = new TreeSet<>();
        set.add(new Person(1, "Spicy strips"));
        set.add(new Person(3, "Popsicle"));
        set.add(new Person(4, "bread"));
        set.add(new Person(2, "Potato chips"));
        set.add(new Person(2, "Potato chips"));
        set.add(new Person(2, "Potato chips"));
        for (Person person : set) {
            System.out.println(person);
        }
    }
}

class Person implements Comparable<Person> {
    public int id;
    public String name;
    public Person(int id, String name) {
        this.id = id;
        this.name = name;
    }
    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + "]";
    }
    @Override
    public int compareTo(Person o) {
        if (o.id - this.id > 0) { //It's designed to be arranged in descending order, with large ones leaning to the left
            return 1;
        }else if (o.id - this.id == 0) {
            return 0;
        }else {
            return -1;
        }
    }
}

Operation result:

 

In addition to having the stored type implement the compatible interface, you can also pass in a comparer when initializing the TreeSet

Here we use a common thing mentioned in the internal class: anonymous internal class!!

import java.util.Comparator;
import java.util.TreeSet;

public class Test {
    public static void main(String[] args) {
        TreeSet<Person> set = new TreeSet<>(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                if (o1.id - o2.id > 0) { // Design in ascending order, large to the right
                    return 1;
                }else if (o1.id - o2.id == 0) {
                    return 0;
                }else {
                    return -1;
                }
            }
        });
        set.add(new Person(1, "Spicy strips"));
        set.add(new Person(3, "Popsicle"));
        set.add(new Person(4, "bread"));
        set.add(new Person(2, "Potato chips"));
        set.add(new Person(2, "Potato chips"));
        set.add(new Person(2, "Potato chips"));
        for (Person person : set) {
            System.out.println(person);
        }
    }
}

class Person {
    public int id;
    public String name;
    public Person(int id, String name) {
        this.id = id;
        this.name = name;
    }
    @Override
    public String toString() {
        return "Person [id=" + id + ", name=" + name + "]";
    }
}

Operation result:

Posted by yumico23 on Thu, 30 Apr 2020 00:43:21 -0700