【Java基础】TreeSet集合、自然排序、比较器排序、成绩排序及不重复随机数案例

目录

一、TreeSet集合概述和特点

二、自然排序Comparable的使用

三、比较器排序Comparator的使用

四、成绩排序案例

五、不重复的随机数案例


一、TreeSet集合概述和特点

TreeSet集合概述:

元素有序,可以按照一定的规则进行排序,具体排序方式取决于构造方法

■ TreeSet():根据其元素的自然排序进行排序

■ TreeSet(Comparator comparator):根据指定的比较器进行排序

没有带索引的方法,所以不能使用普通for循环遍历

由于是Set集合,所以不包括重复元素的集合

TreeSet集合基本使用

public class TreeSetDemo01 { 
    public static void main(String[] args) { 
        //创建集合对象 
        TreeSet<Integer> ts = new TreeSet<Integer>(); 

        //添加元素 
        ts.add(10); 
        ts.add(40); 
        ts.add(30); 
        ts.add(50); 
        ts.add(20); 

        ts.add(30); 

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

二、自然排序Comparable的使用

需求:

存储学生对象并遍历,创建TreeSet集合使用无参构造方法

要求:按照年龄从小到大排序,年龄相同时,按照姓名的字母顺序排序

实现步骤:

● 用TreeSet存储自定义对象,无参构造方法使用的是自然排序对元素进行排序的

● 自然排序,就是让元素多属的类实现Comparable接口,重写compareTo(T o)方法

● 重写方法时,一定要注意排序规则必须按照要求的主要条件和次要条件来写

代码实现:

学生类

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

    public Student() {
    }

    public Student(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(Student s) {
    // return 0;
    // return 1;
    // return -1;
        //按照年龄从小到大排序
        int num = this.age - s.age;
        // int num = s.age - this.age;
        //年龄相同时,按照姓名的字母顺序排序
        int num2 = num == 0 ? this.name.compareTo(s.name) : num;
        return num2;
    }
}

测试类:

public class TreeSetDemo02 {
    public static void main(String[] args) {
        //创建集合对象
        TreeSet<Student> ts = new TreeSet<Student>();

        //创建学生对象
        Student s1 = new Student("xishi", 29);
        Student s2 = new Student("wangzhaojun", 28);
        Student s3 = new Student("diaochan", 30);
        Student s4 = new Student("yangyuhuan", 33);
        Student s5 = new Student("linqingxia", 33);
        Student s6 = new Student("linqingxia", 33);

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

        //遍历集合
        for (Student s : ts) {
            System.out.println(s.getName() + "," + s.getAge());
        }
    }
}

三、比较器排序Comparator的使用

需求:

存储学生对象并遍历,创建TreeSet集合使用带参构造方法

要求:按照年龄从小到大的排序,年龄相同时,按照姓名的字母顺序排序

实现步骤:

● 用TreeSet集合存储自定义对象,带参构造方法使用的是比较器排序对元素进行排序的

● 比较器排序,就是让集合构造方法接收Comparator的实现类对象,重写compare(T o1,T o2)方法

● 重写方法时,一定要注意排序规则必须按照要求的主要条件和次要条件来写

代码实现:

学生类

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

    public Student() {
    }

    public Student(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 class TreeSetDemo {
    public static void main(String[] args) {
        //创建集合对象
        TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                //this.age - s.age
                //s1,s2
                int num = s1.getAge() - s2.getAge();
                int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
                return num2;
            }
        });

        //创建学生对象
        Student s1 = new Student("xishi", 29);
        Student s2 = new Student("wangzhaojun", 28);
        Student s3 = new Student("diaochan", 30);
        Student s4 = new Student("yangyuhuan", 33);

        Student s5 = new Student("linqingxia", 33);
        Student s6 = new Student("linqingxia", 33);

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

        //遍历集合
        for (Student s : ts) {
            System.out.println(s.getName() + "," + s.getAge());
        }
    }
}

四、成绩排序案例

需求:

用TreeSet集合存储多个学生信息(姓名,语文成绩,数学成绩),并遍历该集合

要求:按照总分从高到低出现

代码实现:

学生类

public class Student { 
    private String name; 
    private int chinese; 
    private int math; 

    public Student() { 
    }

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

    public String getName() { 
        return name; 
    }

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

    public int getChinese() { 
        return chinese; 
    }

    public void setChinese(int chinese) { 
        this.chinese = chinese; 
    }

    public int getMath() { 
        return math; 
    }

    public void setMath(int math) { 
        this.math = math; 
    }

    public int getSum() { 
        return this.chinese + this.math; 
    } 
}

测试类:

public class TreeSetDemo {
    public static void main(String[] args) {
        //创建TreeSet集合对象,通过比较器排序进行排序 
        TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {

            @Override
            public int compare(Student s1, Student s2) {
// int num = (s2.getChinese()+s2.getMath())- (s1.getChinese() + s1.getMath());
                //主要条件 
                int num = s2.getSum() - s1.getSum();
                //次要条件 
                int num2 = num == 0 ? s1.getChinese() - s2.getChinese() : num;
                int num3 = num2 == 0 ? s1.getName().compareTo(s2.getName()) : num2;
                return num3;
            }
        });

        //创建学生对象 
        Student s1 = new Student("林青霞", 98, 100);
        Student s2 = new Student("张曼玉", 95, 95);
        Student s3 = new Student("王祖贤", 100, 93);
        Student s4 = new Student("柳岩", 100, 97);
        Student s5 = new Student("风清扬", 98, 98);
        Student s6 = new Student("左冷禅", 97, 99);
        Student s7 = new Student("赵云", 97, 99);

        //把学生对象添加到集合 
        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);
        ts.add(s5);
        ts.add(s6);
        ts.add(s7);

        //遍历集合 
        for (Student s : ts) {
            System.out.println(s.getName() + "," + s.getChinese() + "," +
                    s.getMath() + "," + s.getSum());
        }
    }
}

五、不重复的随机数案例

需求:编写一个程序,获取10个1-20之间的随机数,要求随机数不能重复,并在控制台输出

代码实现:

public class SetDemo { 
    public static void main(String[] args) { 
        //创建Set集合对象 
// Set<Integer> set = new HashSet<Integer>(); 
        Set<Integer> set = new TreeSet<Integer>(); 

        //创建随机数对象 
        Random r = new Random(); 

        //判断集合的长度是不是小于10 
        while (set.size()<10) { 
            //产生一个随机数,添加到集合 
            int number = r.nextInt(20) + 1; 
            set.add(number); 
        }

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

 

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏志121

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值