Set集合的两种排序(自然排序 选择器排序)

转自https://blog.csdn.net/liu_qi_jun/article/details/81988673

TreeSet

一、自然排序

自然排序:也就是自然发生的,在创建集合对象时不需要给参数。注:往TreeSet集合对象中添加元素是,元素必须是可比的(Integer 、Double、Float….),假如是自定义类型Student,就必须让Student实现Comparable接口

自定义类型
package UtilTool;

public class Student implements Comparable<Student> {
    private String name;
    private int age;
    public Student() {
        super();
    }
    public Student(String name, int age) {
        super();
        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 o) {

        return this.age-o.age;    //按年龄排序
    }

}



//测试类
package Study0823;
import java.util.HashSet;
import java.util.Iterator;
import java.util.TreeSet;
import UtilTool.Student;
public class SetDemo {
    public static void main(String[] args) {
        //创建集合对象
        TreeSet<Student> tree = new TreeSet<Student>();
        //创建元素
        Student s1 = new Student("霍建华",15);
        Student s2 = new Student("王亚妮",35);
        Student s3 = new Student("张三疯",25);


        //将元素添加到tree中  注:给TreeSet添加对象时,对象必须是可比的(即实现了Comparable接口),要是不是可比的就不知道将当前对象放在什么位置
                tree.add(s1);
                tree.add(s2);
                tree.add(s3);
        //遍历tree
                Iterator<Student> it = tree.iterator();
                while(it.hasNext()) {
                    Student pre = it.next();
                    System.out.println(pre.getName()+"--"+pre.getAge());
                }
    }
}

二、选择器排序

选择器排序:也就是在创建Set集合对象时给他=它一个选择器,也就是构造函数传参(传一个Comparator的子对象),给集合一个选择排序的标准,具体如何凭借什么选择就看重写Comparator的方法逻辑是怎样实现的

//自定义类  注:这个类对象是不可比的
public class Student  {
    private String name;
    private int age;
    public Student() {
        super();
    }
    public Student(String name, int age) {
        super();
        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;
    }
}

//测试类
package Study0823;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

import UtilTool.Student;
public class SetDemo {
    public static void main(String[] args) {
        //创建集合对象
        TreeSet<Student> tree = new TreeSet<Student>(
            new Comparator<Student>() { //匿名内部类定义匿名对象
                @Override
                public int compare(Student o1, Student o2) {
                    return o1.getAge()-o2.getAge();  //按年龄排序
                }
        });


        Student s1 = new Student("霍建华",15);
        Student s2 = new Student("王亚妮",35);
        Student s3 = new Student("张三疯",25);

                tree.add(s1);
                tree.add(s2);
                tree.add(s3);

        //遍历tree
                Iterator<Student> it = tree.iterator();
                while(it.hasNext()) {
                    Student pre = it.next();
                    System.out.println(pre.getName()+"--"+pre.getAge());
                }
    }
}

Collections.sort()

参考https://www.cnblogs.com/learnapi/p/9003112.html

Arrays.sort()

https://www.cnblogs.com/SupremeBoy/p/12717532.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值