Demo1与Demo2的学生老师类只有姓名和年龄这两个成员变量,学生类重写了compareto(Student o)方法以及toString()方法,老师类也差不多,太过冗余就没上传了。
package com.hncu.myset;
import java.util.Iterator;
import java.util.TreeSet;
public class MySetDemo1 {
//自然排序(优先选择)
//按年龄将学生打印输出,如果年龄一样,按名字排序
// Student{name='xiaohua', age=17}
// Student{name='dahei', age=19}
// Student{name='xiaohei', age=19}
public static void main(String[] args) {
TreeSet<Student> students = new TreeSet<>();
Student s1 = new Student("xiaohei",19);
Student s2 = new Student("dahei",19);
Student s3 = new Student("xiaohua",17);
students.add(s1);
students.add(s2);
students.add(s3);
Iterator<Student> it = students.iterator();
while (it.hasNext()){
Student s = it.next();
System.out.println(s);
}
}
}
package com.hncu.myset;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
public class MySetDemo2 {
//比较器排序
//按年龄将老师打印输出,如果年龄一样,按名字排序
public static void main(String[] args) {
Teacher t1 = new Teacher("wangming", 29);
Teacher t2 = new Teacher("sunhong", 30);
Teacher t3 = new Teacher("zhangli", 29);
// TreeSet<Teacher> ts = new TreeSet<>(new Comparator<Teacher>() {
// @Override
// public int compare(Teacher o1, Teacher o2) {
// int result = o1.getAge() - o2.getAge();
// result = result == 0 ? o1.getName().compareTo(o2.getName()) : result;
// return result;
// }
// });
//lamda
TreeSet<Teacher> ts = new TreeSet<>(
(Teacher o1, Teacher o2) -> {
int result = o1.getAge() - o2.getAge();
result = result == 0 ? o1.getName().compareTo(o2.getName()) : result;
return result;
}
);
ts.add(t1);
ts.add(t2);
ts.add(t3);
Iterator<Teacher> it = ts.iterator();
while (it.hasNext()) {
Teacher t = it.next();
System.out.println(t);
}
}
}
package com.hncu.treesettest;
public class Student implements Comparable<Student>{
private String name;
private int age;
private int chinese;
private int math;
private int english;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", chinese=" + chinese +
", math=" + math +
", english=" + english +
'}'+ "总成绩为:"+ getsum();
}
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 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 Student(String name, int age, int chinese, int math, int english) {
this.name = name;
this.age = age;
this.chinese = chinese;
this.math = math;
this.english = english;
}
public Student() {
}
public int getsum(){
return chinese + math +english;
}
@Override
public int compareTo(Student o) {
//成绩比较
int result = o.getsum() - this.getsum();
//成绩一样比语文
result = result == 0 ? o.chinese - this.chinese : result;
//语文一样比数学
result = result == 0 ? o.math - this.math : result;
//全是一样,按姓名
result = result == 0 ? o.getName().compareTo(this.getName()) : result;
return result;
}
}
package com.hncu.treesettest;
import java.util.TreeSet;
public class TreeSetDemo {
//按总分将学生打印输出,如果总分一样,按各科目(语文,数学,英语)排序,各科目分数一样按姓名排序
//Student{name='dahei', age=19, chinese=100, math=100, english=100}总成绩为:300
//Student{name='xiaoming', age=19, chinese=90, math=90, english=90}总成绩为:270
//Student{name='xiaojun', age=19, chinese=86, math=85, english=84}总成绩为:255
//Student{name='dajun', age=19, chinese=85, math=86, english=84}总成绩为:255
//Student{name='xiaohei', age=19, chinese=85, math=85, english=85}总成绩为:255
//Student{name='dagang', age=19, chinese=85, math=85, english=85}总成绩为:255
//Student{name='daming', age=19, chinese=80, math=80, english=80}总成绩为:240
public static void main(String[] args) {
Student s1 = new Student("daming", 19, 80, 80, 80);
Student s2 = new Student("xiaoming", 19, 90, 90, 90);
Student s3 = new Student("dahei", 19, 100, 100, 100);
Student s4 = new Student("xiaohei", 19, 85, 85, 85);
Student s5 = new Student("dajun", 19, 85, 86, 84);
Student s6 = new Student("xiaojun", 19, 86, 85, 84);
Student s7 = new Student("dagang", 19, 85, 85, 85);
TreeSet<Student> ts = new TreeSet<>();
ts.add(s1);
ts.add(s2);
ts.add(s3);
ts.add(s4);
ts.add(s5);
ts.add(s6);
ts.add(s7);
for (Student t : ts) {
System.out.println(t);
}
}
}