Java教程

TreeSet集合

本文主要是介绍TreeSet集合,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

特点

  • 不可以存储重复元素
  • 没有索引
  • 可以将元素按照指定规则进行排序

自然排序Comparable的使用

  • 要求按照年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序
package com.ding.mytreeset;

/**
 * @Description TODO
 * @Author 丁帅帅
 * @Date 21/10/07 22:25
 * @Version 1.0
 */
public class Student implements Comparable<Student>{
    private String name;
    private  int age;

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

    public Student() {
    }

    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 String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public int compareTo(Student o) {
        //按照对象的年龄进行排序
        //主要判断条件
        //this是当前对象,o是已存在的对象
        //小的放左边
        int result = this.age - o.age;
        //次要判断条件
        result = result == 0 ? this.name.compareTo(o.getName()) : result;
        return result;
    }
}

package com.ding.mytreeset;

import java.util.TreeSet;

/**
 * @Description TreeSet集合来存储Student类型
 * @Author 丁帅帅
 * @Date 21/10/07 22:24
 * @Version 1.0
 */
public class MyTreeSet {
    public static void main(String[] args) {
        TreeSet<Student> ts = new TreeSet<Student>();

        Student s1 = new Student("aa",28);
        Student s2 = new Student("lisi",27);
        Student s3 = new Student("wangwu",29);
        Student s4 = new Student("bb",28);
        Student s5 = new Student("qianqi",30);

        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);
        ts.add(s5);

        System.out.println(ts);
    }
}

结果

[Student{name='lisi', age=27}, Student{name='aa', age=28}, Student{name='bb', age=28}, Student{name='wangwu', age=29}, Student{name='qianqi', age=30}]

比较器排序Comparator的使用

  • 要求: 按照年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序
package com.ding.mytreeset;

/**
 * @Description TODO
 * @Author 丁帅帅
 * @Date 21/10/07 22:42
 * @Version 1.0
 */
public class Teacher {
    private String name;
    private int age;

    public Teacher() {
    }

    public Teacher(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 String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

package com.ding.mytreeset;

import java.util.Comparator;
import java.util.TreeSet;

/**
 * @Description TODO
 * @Author 丁帅帅
 * @Date 21/10/07 22:42
 * @Version 1.0
 */
public class MyTreeSetTeacher {
    public static void main(String[] args) {
        TreeSet<Teacher> ts = new TreeSet<Teacher>(new Comparator<Teacher>() {
            public int compare(Teacher o1, Teacher o2) {

                //o1表示现在要存入的那个元素
                //o2表示已经存入到集合中的元素

                //主要条件
                int result = o1.getAge() - o2.getAge();
                //次要条件
                result = result == 0 ? o1.getName().compareTo(o2.getName()) :result;
                return  result;
            }
        });

        Teacher t1 = new Teacher("zhangsan",23);
        Teacher t2 = new Teacher("lisi",22);
        Teacher t3 = new Teacher("aa",24);
        Teacher t4 = new Teacher("bb",24);

        ts.add(t1);
        ts.add(t2);
        ts.add(t3);
        ts.add(t4);

        System.out.println(ts);

    }
}

结果

[Teacher{name='lisi', age=22}, Teacher{name='zhangsan', age=23}, Teacher{name='aa', age=24}, Teacher{name='bb', age=24}]

这篇关于TreeSet集合的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!