Java集合-TreeSet 集合

一、TreeSet 集合

1.1、TreeSet 集合概述和特点

1.1.1、TreeSet 集合概述

  • TreeSet 集合间接继承了 Set 接口。

1.1.2、TreeSet 集合特点

  • 元素有序,这里的顺序不是指存储和取出的顺序,而是按照一定的规则进行排序,具体排序方式取决于构造方法。
    • TreeSet():根据其元素的自然排序进行排序。
    • TreeSet(Comparator comparator):根据指定的比较器进行排序。
  • 没有带索引的方法,所以不能使用普通 for 循环遍历。
  • 由于是 Set 集合,所以不包含重复元素的集合。

1.2、使用 TreeSet 集合

// 创建集合对象
TreeSet<Integer> it = new TreeSet<>();

// 添加元素
it.add(10); // JDK5 之后,实现了自动装箱
it.add(200);
it.add(30);

// 遍历
for (Integer i : it){
    System.out.println(i);
}

1.3、TreeSet() 使用自然排序

  • 需求:
    • 存储学生对象并遍历,创建 TreeSet 集合使用无参构造方法
  • 要求:
    • 按照年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序。
// 学生类

// 想要使用 自然排序 需要实现 Comparable<E> 接口
public class Student1 implements Comparable<Student1>{
    private String name;
    private int age;

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

    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; }

    @Override
    public int compareTo(Student1 o) {
//        return 0; 返回 0 表示,相同元素不添加
//        return 1; 返回 正数 表示按照升序添加
//        return -1; 返回 负数 按照降序添加

        // 按照年龄从小到大
        int i = this.age - o.age;
        // 年龄相同比姓名
        int num = i== 0 ? this.name.compareTo(o.name) : i;
        return num;
    }
}
// 测试类

public static void main(String[] args) {

  // 创建集合对象
  TreeSet<Student1> st = new TreeSet<>();

  // 创建学生对象
  Student1 s1 = new Student1("王昭君",29);
  Student1 s2 = new Student1("西施",28);
  Student1 s3 = new Student1("貂蝉",19);
  Student1 s4 = new Student1("杨玉环",32);
  Student1 s5 = new Student1("杨环",32);

  // 把学生添加到集合中
  st.add(s1);
  st.add(s2);
  st.add(s3);
  st.add(s4);
  st.add(s5);

  // 遍历集合
  for(Student1 s : st){
      System.out.println(s.getName() + ", " + s.getAge());
  }
}
  • 结论:
    • 用 TreeSet 集合存储自定义对象,无参构造方法使用的是自然排序对元素进行使用的
    • 自然排序,就是让元素所属的类实现 Comparable 接口,重写 compareTo(T o) 方法
    • 重写方法时,一定要注意排序规则必须按照要求的主要条件和次要条件来写。

1.4、TreeSet(Comparator comparator) 使用指定比较器

  • 需求:
    • 存储学生对象并遍历,创建 TreeSet 集合使用无参构造方法
  • 要求:
    • 按照年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序。
// 学生类

public class Student1{
    private String name;
    private int age;

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

    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 static void main(String[] args) {

  // 创建集合对象   并指定比较器
  TreeSet<Student1> st = new TreeSet<>(new Comparator<Student1>() {
      @Override
      public int compare(Student1 o1, Student1 o2) {
          int num = o1.getAge() - o2.getAge();
          int num2 = num == 0 ? o1.getName().compareTo(o2.getName()) : num;
          return num2;
      }
  });

  // 创建学生对象
  Student1 s1 = new Student1("王昭君",29);
  Student1 s2 = new Student1("西施",28);
  Student1 s3 = new Student1("貂蝉",19);
  Student1 s4 = new Student1("杨玉环",32);
  Student1 s5 = new Student1("杨环",32);

  // 把学生添加到集合中
  st.add(s1);
  st.add(s2);
  st.add(s3);
  st.add(s4);
  st.add(s5);

  // 遍历集合
  for(Student1 s : st){
      System.out.println(s.getName() + ", " + s.getAge());
  }
}
  • 结论:
    • 用 TreeSet 集合存储自定义对象,带参构造方法使用的是比较器排序对元素进行排序的
    • 比较器排序,就是让集合构造方法接收 Comparator 的实现类对象,重写 compare(T o,T 02) 方法
    • 重写方法时,一定要注意排序规则必须按照要求的主要条件和次要条件来写
posted @ 2021-02-20 15:50    阅读(185)  评论(0)    收藏  举报