Skip to the content.

王孝东的个人空间

TreeSet排序总结

TreeSet排序常用的两种方式:

  1. 通过TreeSet(Comparator<? super E> comparator)构造方法指定TreeSet的比较器进行排序
  2. 使用TreeSet()构造方法,并对需要添加到set集合中的元素实现Comparable接口进行排序

下面通过示例代码详细介绍这两种方法:

1. 通过TreeSet(Comparator<? super E> comparator) 构造方法指定TreeSet的比较器进行排序

package src

public class Foo {
    private int num;

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public String toString() {
        return "foo:" + this.getNum() + ",";
    }
}
package src

import java.util.Comparator;

public class MyComparator implements Comparator<Foo> {
    public int compare(Foo f1, Foo f2) {
        if (f1.getNum() > f2.getNum()) {
            return 1;
        } else if (f1.getNum() == f2.getNum()) {
            return 0;
        } else {
            return -1;
        }
    }
}
TreeSet<Foo> set = new TreeSet(new MyComparator());

这样在set.add()元素时就会根据自己定义比较器进行排序了

2. 使用TreeSet()构造方法,并对需要添加到set集合中的元素实现Comparable接口进行排序

这种方法不需要自己写一个比较器,需要对装入set集合中的元素实现Comparable接口,TreeSet集合就根据bean的自然顺序进行排序

public class Foo implements Comparable {
    private int num;

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public String toString() {
        return "foo:" + this.getNum() + ",";
    }

    public int compareTo(Object obj) {
        if (obj instanceof Foo) {
            Foo foo = (Foo) obj;
            if (this.num > foo.getNum()) {
                return 1;
            } else if (this.num == foo.getNum()) {
                return 0;
            } else {
                return -1;
            }

        }
        return 0;
    }
}
TreeSet<Foo> set = new TreeSet(); 

不需要指定比较器,这样在执行set.add()方法时,set集合就自动根据bean中compareTo()方法指定的方式进行排序。

两种方法任选其一都能达到目的