嘮嘮SE的集合-08——TreeSet

8. TreeSet

使用元素的天然順序對元素進行排序,或者根據建立set時提供的Comparator進行排序。java

底層數據結構是紅黑樹(紅黑樹是一種自平衡的二叉樹,特色是左大右小)數據結構

有關紅黑樹的知識,戳:紅黑樹, 或者:最容易懂的紅黑樹ide

 

咱們在HashSet裏寫過一個Person,那咱們直接拿來存到TreeSet裏吧:this

import java.util.TreeSet;

public class Test {
    public static void main(String[] args) {
        TreeSet<Person> set = new TreeSet<>();
        set.add(new Person(1, "辣條"));
        set.add(new Person(3, "冰棍"));
        set.add(new Person(4, "麪包"));
        set.add(new Person(2, "薯片"));
        set.add(new Person(2, "薯片"));
        set.add(new Person(2, "薯片"));
        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 + "]";
    }
}

運行以後報了一個錯。。。spa

注意!!!.net

TreeSet存儲的類型必須實現Comparable接口並重寫compareTo方法,不然會拋出ClassCastException設計

 

那咱們讓Person類實現Comparable接口,再存儲到TreeSet中:code

(注意,這裏只能返回-1,0,1  三個值)blog

import java.util.TreeSet;

public class Test {
    public static void main(String[] args) {
        TreeSet<Person> set = new TreeSet<>();
        set.add(new Person(1, "辣條"));
        set.add(new Person(3, "冰棍"));
        set.add(new Person(4, "麪包"));
        set.add(new Person(2, "薯片"));
        set.add(new Person(2, "薯片"));
        set.add(new Person(2, "薯片"));
        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) { //設計的是降序排列,大的往左靠
            return 1;
        }else if (o.id - this.id == 0) {
            return 0;
        }else {
            return -1;
        }
    }
}

運行結果:排序

 

除了讓存入的類型實現Comparable接口外,還能夠在初始化TreeSet時傳入一個比較器

這裏用到了以前在內部類裏提到的一個比較經常使用的東西:匿名內部類!!

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) { // 設計的升序排列,大的往右靠
                    return 1;
                }else if (o1.id - o2.id == 0) {
                    return 0;
                }else {
                    return -1;
                }
            }
        });
        set.add(new Person(1, "辣條"));
        set.add(new Person(3, "冰棍"));
        set.add(new Person(4, "麪包"));
        set.add(new Person(2, "薯片"));
        set.add(new Person(2, "薯片"));
        set.add(new Person(2, "薯片"));
        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 + "]";
    }
}

運行結果:

相關文章
相關標籤/搜索