TreeSet集合對對象引用數據類型的排序

創建一個Student類,此處需要注意的就是Set集合是不可以存放重複元素的,所以我們爲了遵循此規則,讓存儲的對象不重複我們就需要在Student對象中重寫hashCode()方法和equals()方法

第一種通過實現Compatable接口
//Student類

import java.util.Objects;

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

    public Student() {
    }

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

    @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 &&
                secore == student.secore &&
                Objects.equals(name, student.name);
    }

    @Override
    public int hashCode() {

        return Objects.hash(name, age, secore);
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", secore=" + secore +
                '}'+'\n';
    }


    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 int getSecore() {
        return secore;
    }

    public void setSecore(int secore) {
        this.secore = secore;
    }

    //重寫compareTo()方法
    @Override
    public int compareTo(Student o) {
        return o.secore - this.secore;  //通過分數降序排序
    }
}

//測試類一
package SetDemo;

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

public class TreeSetObjSort {
public static void main(String[] args) {
//通過匿名內部類的方式重寫compare()方法,改寫排序規則,通過分數升序排序

    //對於Set集合需要注意的就是,想實現不存儲重複的對象,那麼就需要在Student對象中
    // 重寫equals()方法與hashCode()方法
    TreeSet<Student> tree  = new TreeSet<>();

    tree.add(new Student("zahngsan",18,89));
    tree.add(new Student("lisi",27,52));
    tree.add(new Student("wuhua",20,68));
    tree.add(new Student("iuys",35,74));
    System.out.println(tree);
}

}

//運行結果

[Student{name='zahngsan', age=18, secore=89}
, Student{name='iuys', age=35, secore=74}
, Student{name='wuhua', age=20, secore=68}
, Student{name='lisi', age=27, secore=52}
]

//測試類二,通過匿名內部類

import java.util.TreeSet;

public class TreeSetObjSort {
    public static void main(String[] args) {
        //通過匿名內部類的方式重寫compare()方法,改寫排序規則,通過分數升序排序

        //對於Set集合需要注意的就是,想實現不存儲重複的對象,那麼就需要在Student對象中
        // 重寫equals()方法與hashCode()方法
        TreeSet<Student> tree  = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getSecore() - o1.getSecore();
            }
        });

        tree.add(new Student("zahngsan",18,89));
        tree.add(new Student("lisi",27,52));
        tree.add(new Student("wuhua",20,68));
        tree.add(new Student("iuys",35,74));
        System.out.println(tree);
    }

}

//運行結果

[Student{name='zahngsan', age=18, secore=89}
, Student{name='iuys', age=35, secore=74}
, Student{name='wuhua', age=20, secore=68}
, Student{name='lisi', age=27, secore=52}
]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章