当前位置: 移动技术网 > IT编程>开发语言>Java > java TreeSet

java TreeSet

2020年04月06日  | 移动技术网IT编程  | 我要评论

treeset可以对set集合中的元素进行排序。

底层数据结构:二叉树。

保证元素唯一:comoareto  return 0;

treeset排序的第一种方式:让元素自身具备比较性。元素实现comparable接口覆盖compareto方法,这种方式也称为元素的自然顺序,或者叫默认顺序。

 

treeset排序的第二种方式:当元素自身不具备比较性时,或者具备的比较性不是所需要的,这时需要让集合自身具备比较性。在集合初始化时,就有了比较方式。

 

存储自定义对象学生。

记住:排序时,当主要条件相同时,一定要判断一下次要条件。

public class treesetdemo {
    public static void main(string[] args) {
        treeset ts = new treeset();
        ts.add(new student("lisi02", 2));
        ts.add(new student("lisi01", 1));
        ts.add(new student("lisi04", 4));
        ts.add(new student("lisi03", 3));
        ts.add(new student("lisi04", 3));
        iterator it = ts.iterator();
        while (it.hasnext()) {
            student s = (student) it.next();
            system.out.println(s.getname() + "::" + s.getage());
        }
    }
}

class student implements comparable {
    private string name;
    private int age;

    public student(string name, int age) {
        this.name = name;
        this.age = age;
    }

    public string getname() {
        return name;
    }

    public int getage() {
        return age;
    }

    @override
    public int compareto(object o) {
        if (!(o instanceof student))
            throw new runtimeexception("不是学生对象");
        student s = (student) o;
        if (this.age > s.age) {
            return 1;
        }
        if (this.age == s.age) {
            return this.name.compareto(s.name);
        }
        return -1;
    }
}

 

存入和取出顺序一样:comoareto  return  正数

存入和取出顺序相反:comoareto  return  负数

只存入一个元素:comoareto  return  0

 

当元素自身不具备比较性,或者具备的比较性不是所需要的,这时需要让容器自身具备比较性,定义比较器,将比较器对象作为参数传递给treeset集合的构造函数。

 

public class treesetdemo {
    public static void main(string[] args) {
        treeset ts = new treeset(new mycompare());
        ts.add(new student("lisi02", 2));
        ts.add(new student("lisi01", 1));
        ts.add(new student("lisi04", 4));
        ts.add(new student("lisi03", 3));
        ts.add(new student("lisi04", 3));
        iterator it = ts.iterator();
        while (it.hasnext()) {
            student s = (student) it.next();
            system.out.println(s.getname() + "::" + s.getage());
        }
    }
}

class student {
    private string name;
    private int age;

    public student(string name, int age) {
        this.name = name;
        this.age = age;
    }

    public string getname() {
        return name;
    }

    public int getage() {
        return age;
    }
}

class mycompare implements comparator {

    @override
    public int compare(object o1, object o2) {
        student s1 = (student) o1;
        student s2 = (student) o2;
        int num = s1.getname().compareto(s2.getname());
        if (num == 0)
            return s1.getage() - s2.getage();
        return num;
    }
}

 

当两种方式都存在时以比较器为主。

比较器:定义一个类,实现comparetor接口,覆盖compare方法。

 

存储字符串并按长度排序:

字符串本身具备比较性,但它的比较性不是我们所需要的。使用比较器。

public class treesetdemo {
    public static void main(string[] args) {
        treeset ts = new treeset(new mycompare());
        ts.add("sd");
        ts.add("q");
        ts.add("qq");
        ts.add("hsaa");
        ts.add("ehhhhh");
        iterator it = ts.iterator();
        while (it.hasnext()) {
            system.out.println(it.next());
        }
    }
}

class mycompare implements comparator {

    @override
    public int compare(object o1, object o2) {
        string s1 = (string) o1;
        string s2 = (string) o2;
        int num = new integer(s1.length()).compareto(new integer(s2.length()));
        if (num == 0) {
            return s1.compareto(s2);
        }
        return num;
    }
}

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网