Java-集合(补充)-Set

Java-集合(补充)-Set

Set应用举例

1.HashSet案例

产生10个1-100之间的随机数要求随机数不能重复
分析:不重复性----HashSet集合特性、应用Random产生随机数

    javascript    11行

public static void main(String[] args) {
        Random random = new Random();
        HashSet<Object> hashSet = new HashSet<>();
        while (hashSet.size() < 10) {
            
            hashSet.add(random.nextInt(100) + 1);//产生随机数并存入HashSet集合使随机数唯一(控制随机数范围1-100)
        }
        System.out.println(hashSet);

    }

1.TreeSet自然排序与比较器排序

创建学生信息类
对comparable()接口中compareTo()方法重写

    javascript    60行

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

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return age == student.age &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {

        return Objects.hash(name, age);
    }
//根据学生年龄大小、姓名字典顺序和长度三个方面优先顺序进行排序
    @Override
    public int compareTo(Student o) {
        int num = this.age - o.getAge();//年龄大小
        int num1 = num == 0? this.name.compareTo(o.name):num;//姓名字典排序
        int num2 = num1 == 0? this.name.length()- o.name.length():num1;//姓名长度
        return num2;
    }
}

将学生信息存入TreeSet集合中并用自然排序方法排序

    javascript    14行

 public static void main(String[] args) {
        TreeSet<Student> treeSet = new TreeSet<>();
        treeSet.add(new Student("张三123123", 22));
        treeSet.add(new Student("李四123", 20));
        treeSet.add(new Student("王五123123123", 19));
        treeSet.add(new Student("赵六12", 21));
        treeSet.add(new Student("赵六123", 21));
        treeSet.add(new Student("杨三123123", 22));
        treeSet.add(new Student("宋三123123", 22));
        for (Student student : treeSet) {
            System.out.println(student.toString());
        }
    }

重写comparator()接口中的compare()方法
将学生信息存入TreeSet集合中并用比较器排序

    javascript    22行

public static void main(String[] args) {
        TreeSet<Student> treeSet = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int num = o1.getAge() - o2.getAge();
                int num1 = num == 0 ? o1.getName().compareTo(o2.getName()) : num;
                int num2 = num1 == 0 ? o1.getName().length() - o2.getName().length() : num1;
                return num2;
            }
        });
        treeSet.add(new Student("张三123123", 22));
        treeSet.add(new Student("李四123", 20));
        treeSet.add(new Student("王五123123123", 19));
        treeSet.add(new Student("赵六12", 21));
        treeSet.add(new Student("赵六123", 21));
        treeSet.add(new Student("杨三123123", 22));
        treeSet.add(new Student("宋三123123", 22));
        for (Student student : treeSet) {
            System.out.println(student.toString());
        }
    }