9.25模擬賽
阿新 • • 發佈:2021-09-30
特點
- 不可以儲存重複元素
- 沒有索引
- 可以將元素按照指定規則進行排序
自然排序Comparable的使用
- 要求按照年齡從小到大排序,年齡相同時,按照姓名的字母順序排序
package com.ding.mytreeset; /** * @Description TODO * @Author 丁帥帥 * @Date 21/10/07 22:25 * @Version 1.0 */ 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 + '}'; } public int compareTo(Student o) { //按照物件的年齡進行排序 //主要判斷條件 //this是當前物件,o是已存在的物件 //小的放左邊 int result = this.age - o.age; //次要判斷條件 result = result == 0 ? this.name.compareTo(o.getName()) : result; return result; } }
package com.ding.mytreeset; import java.util.TreeSet; /** * @Description TreeSet集合來儲存Student型別 * @Author 丁帥帥 * @Date 21/10/07 22:24 * @Version 1.0 */ public class MyTreeSet { public static void main(String[] args) { TreeSet<Student> ts = new TreeSet<Student>(); Student s1 = new Student("aa",28); Student s2 = new Student("lisi",27); Student s3 = new Student("wangwu",29); Student s4 = new Student("bb",28); Student s5 = new Student("qianqi",30); ts.add(s1); ts.add(s2); ts.add(s3); ts.add(s4); ts.add(s5); System.out.println(ts); } }
結果
[Student{name='lisi', age=27}, Student{name='aa', age=28}, Student{name='bb', age=28}, Student{name='wangwu', age=29}, Student{name='qianqi', age=30}]
比較器排序Comparator的使用
- 要求: 按照年齡從小到大排序,年齡相同時,按照姓名的字母順序排序
package com.ding.mytreeset; /** * @Description TODO * @Author 丁帥帥 * @Date 21/10/07 22:42 * @Version 1.0 */ public class Teacher { private String name; private int age; public Teacher() { } public Teacher(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 "Teacher{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
package com.ding.mytreeset;
import java.util.Comparator;
import java.util.TreeSet;
/**
* @Description TODO
* @Author 丁帥帥
* @Date 21/10/07 22:42
* @Version 1.0
*/
public class MyTreeSetTeacher {
public static void main(String[] args) {
TreeSet<Teacher> ts = new TreeSet<Teacher>(new Comparator<Teacher>() {
public int compare(Teacher o1, Teacher o2) {
//o1表示現在要存入的那個元素
//o2表示已經存入到集合中的元素
//主要條件
int result = o1.getAge() - o2.getAge();
//次要條件
result = result == 0 ? o1.getName().compareTo(o2.getName()) :result;
return result;
}
});
Teacher t1 = new Teacher("zhangsan",23);
Teacher t2 = new Teacher("lisi",22);
Teacher t3 = new Teacher("aa",24);
Teacher t4 = new Teacher("bb",24);
ts.add(t1);
ts.add(t2);
ts.add(t3);
ts.add(t4);
System.out.println(ts);
}
}
結果
世界不會因為你的疲憊,而停下它的腳步[Teacher{name='lisi', age=22}, Teacher{name='zhangsan', age=23}, Teacher{name='aa', age=24}, Teacher{name='bb', age=24}]