一、TreeSet:
底層資料結構是紅黑樹(自平衡二叉樹),具備了可預測的排序
1.自然排序
透過實現comparable介面,重寫裡面的compareTo方法來進行排序
1.編寫一個Dog類,實現了Comparable介面,並重寫裡面的方法
public class Dog implements Comparable<Dog>{
private String name;
private int age;
public Dog() {
}
public Dog(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 "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public int compareTo(Dog o) {
// this - 待插入元素
// o - 要比較的根元素
//當姓名和年齡一樣的時候去重,年齡需要從小到大排序
//return this.age - o.getAge();
//顯式條件
int i=this.age-o.getAge();
//隱式條件
//當年齡一樣的時候,姓名不一定一樣
return (i==0)?this.name.compareTo(o.getName()) :i;
}
}
2.測試類
import java.util.TreeSet;
public class TreeSetDemo2 {
public static void main(String[] args) {
TreeSet<Dog> treeSet = new TreeSet<>();
Dog d1 = new Dog("煤球",3);
Dog d2 = new Dog("旺財",2);
Dog d3 = new Dog("小灰",3);
Dog d4 = new Dog("煤球",3);
Dog d5 = new Dog("白線",4);
treeSet.add(d1);
treeSet.add(d2);
treeSet.add(d3);
treeSet.add(d4);
treeSet.add(d5);
//當傳入一個物件的成員變數值的時候,我們要實現Comparable介面,並重寫compareTo方法,裡面的內容決定按照什麼排序
System.out.println(treeSet);
}
}
2.比較器排序
可以不實現comparable介面,傳遞一個 Comparator 物件
1.先編寫一個Dog1類
package com.day14;
public class Dog1 {
private String name;
private int age;
public Dog1() {
}
public Dog1(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 "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
2.再編寫測試類
import java.util.Comparator;
import java.util.TreeSet;
public class TreeSetDemo3 {
public static void main(String[] args) {
//使用比較器排序,匿名內部類
TreeSet<Dog1> treeSet = new TreeSet<>(new Comparator<Dog1>() {
@Override
public int compare(Dog1 o1, Dog1 o2) {
int i=o1.getAge()- o2.getAge();
return (i==0)?o1.getName().compareTo(o2.getName()):i;
}
});
Dog1 d1 = new Dog1("煤球",3);
Dog1 d2 = new Dog1("旺財",2);
Dog1 d3 = new Dog1("小灰",3);
Dog1 d4 = new Dog1("煤球",3);
Dog1 d5 = new Dog1("白線",4);
treeSet.add(d1);
treeSet.add(d2);
treeSet.add(d3);
treeSet.add(d4);
treeSet.add(d5);
//當傳入一個物件的成員變數值的時候,我們要實現Comparable介面,並重寫compareTo方法,裡面的內容決定按照什麼排序
System.out.println(treeSet);
}
}
二、TreeSet的練習題:
鍵盤錄入5個學生資訊(姓名,語文成績,數學成績,英語成績),按照總分從高到低輸出到控制檯
1.先編寫一個學生類,注意一下這個學生類我們需要編寫一個方法用來獲取總分
public class Student2 {
//姓名,語文成績,數學成績,英語成績
private String name;
private int chinese;
private int math;
private int english;
public Student2() {
}
public Student2(String name, int chinese, int math, int english) {
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
}
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 getEnglish() {
return english;
}
public void setEnglish(int english) {
this.english = english;
}
public int getSumScore() {
return chinese + math + english;
}
@Override
public String toString() {
return "Student2{" +
"name='" + name + '\'' +
", chinese=" + chinese +
", math=" + math +
", english=" + english +
'}';
}
}
2.編寫測試類,這個測試類需要去進行排序,按照年齡的大小,這裡使用比較器排序,就不用實現comparable介面,重寫裡面的方法
public class TreeSetTest1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//建立一個TreeSet集合
TreeSet<Student2> set1 = new TreeSet<>(new Comparator<Student2>() {
@Override
public int compare(Student2 o1, Student2 o2) {
//顯式條件:按照總分從高到低輸出到控制檯
int i1 = o2.getSumScore() - o1.getSumScore();
//隱式條件
//總分一樣,語文成績不一定一樣
int i2 = (i1 == 0) ? o2.getChinese() - o1.getChinese() : i1;
//總分一樣,語文成績一樣,數學成績不一定一樣
int i3 = (i2 == 0) ? o2.getMath() - o1.getMath() : i2;
//各科成績一樣,姓名不一定一樣
return (i3 == 0) ? o2.getName().compareTo(o1.getName()) : i3;
}
});
for(int i=1;i<=5;i++){
System.out.println("請輸入第 "+i+" 個學生的資訊");
System.out.print("請輸入姓名: ");
String name = sc.next();
System.out.print("請輸入該學生的語文成績: ");
int chinese = sc.nextInt();
System.out.print("請輸入該學生的數學成績: ");
int math = sc.nextInt();
System.out.print("請輸入該學生的英語成績: ");
int english = sc.nextInt();
set1.add(new Student2(name,chinese,math,english));
}
System.out.println("學生資訊錄入完畢!!");
System.out.println("================= 學生成績彙總 ===================");
System.out.println("姓名\t\t語文成績\t\t數學成績\t\t英語成績\t\t總分");
//遍歷集合
for (Student2 student2 : set1) {
System.out.println(student2.getName()+"\t\t"+
student2.getChinese()+"\t\t"+
student2.getMath()+"\t\t"+
student2.getEnglish()+"\t\t"+
student2.getSumScore());
}
}
}