您当前的位置: 首页 > 
  • 2浏览

    0关注

    674博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Treeset保证元素唯一且排序的代码示例

沙漠一只雕得儿得儿 发布时间:2017-04-05 21:02:18 ,浏览量:2

  1. TreeSet:根据构造方法的不用,选择使用自然排序或者比较器排序。  按照实际的需求,可以对元素进行排序。并且保证唯一。
  2. 怎么保证的呢?  排序:底层结构是二叉树。按照树节点进行存储和取出。  两种实现:  A:自然排序(元素具备比较性)  TreeSet的无参构造,要求对象所属的类实现Comparable接口。  B:比较器排序(集合具备比较性)  TreeSet的带参构造,要求构造方法接收一个实现了Comparator接口的对象。  唯一:根据返回值是否为0。  注意:  如果同时有两种方案,以谁为主呢?以比较器为主。  以比较器排序为主
方式一对存进集合的对象所属的类实现Comparable接口,进行自然排序
package cn.liuwen.treeset;

/**
     * 需求:用TreeSet集合存储3个个Student对象, 然后遍历集合.(使用自然排序,按照元素添加顺序存储)
     * @author lwen
     * @version 
     * @date 2015年7月7日
     * 需求2:按照对象的姓名长度,从小到大排序,怎么做?
     */

public class Student implements Comparable{
private String name;
private int age;

public Student() {
}

public Student(String name, int age) {
    this.name = name;
    this.age = age;
}

public void setName(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

public void setAge(int age) {
    this.age = age;
}

public int getAge() {
    return age;
}

//方式一,在要存入元素的那个对象类里实现Comparable接口
//重写CompareTo()方法
@Override
public int compareTo(Student o) {
    Student s =  o; // 将比较对象强转为Student类型
    if (this.name.length() - s.name.length() > 0) { // 定义比较方式
        return 1;
    }

    if (this.name.length() - s.name.length() == 0) {
        return this.name.compareTo(s.name);// 将比较结果返回
    }
    return -1;
    }
}

//在主方法中传入上面的类对象
public class TreeSetDemo1 {
public static void main(String[] args) {
    TreeSet ts = new TreeSet();
    ts.add(new Student("liuwen",22));
    ts.add(new Student("lifuwen",22));
    ts.add(new Student("liuwen",22));
    ts.add(new Student("guixin",12));
    ts.add(new Student("guixiangd",18));
    for (Student s:ts) {
        System.out.println(s.getName()+"****"+s.getAge());

    }
}
}
方式二TreeSet的带参构造,要求构造方法接收一个实现了Comparator接口的对象。
public class Student2  {
private String name;
private int age;
private int score;

public Student2() {
    super();
    // TODO Auto-generated constructor stub
}

public Student2(String name, int age, int score) {
    super();
    this.name = name;
    this.age = age;
    this.score = score;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public int getScore() {
    return score;
}

public void setScore(int score) {
    this.score = score;
}

//在主方法中传入这个类对象
public class TreeSetDemo3 {
public static void main(String[] args) {
    //使用匿名内部类的方式,重写compare方法,在这个方法里对对象进行排序
    TreeSet ts = new TreeSet(new Comparator() {

        @Override
        public int compare(Student2 s1, Student2 s2) {
            int num = s2.getScore()-s1.getScore();
            //定义排序方式
            int num2 = (num==0)?(s1.getAge()-s2.getAge()):(num);
            int num3 = (num2==0)?(s1.getName().compareTo(s2.getName())):(num2);
            return num3;
        }
    });
    ts.add(new Student2("liuwen",12,13));
    ts.add(new Student2("liuwen",12,12));
    ts.add(new Student2("liuwen",12,14));
    ts.add(new Student2("lauwen",12,14));
    ts.add(new Student2("liuwen",13,14));
    ts.add(new Student2("liuwen",14,14));
    ts.add(new Student2("lauwen",12,13));
    for(Student2 s:ts){
        System.out.println(s.getName()+"***"+s.getAge()+"***"+s.getScore());
    }
}
}
关注
打赏
1657159701
查看更多评论
立即登录/注册

微信扫码登录

0.0566s