学生成绩排序(比较器Comparator排序)
package com.liushuaishuai; import java.util.Comparator; import java.util.TreeSet; public class TreeSetDemo { public static void main(String[] args) { TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() { @Override public int compare(Student s1, Student s2) { int num = s2.getSum()-s1.getSum(); int num2 = (num == 0 ? s1.getName().compareTo(s2.getName()):num); return num2; } }); Student s1 = new Student("风清扬",95,98); Student s2 = new Student("流清扬",94,98); Student s3 = new Student("李清扬",93,98); Student s4 = new Student("B清扬",92,98); Student s5 = new Student("A清扬",92,98); ts.add(s1);ts.add(s2);ts.add(s3);ts.add(s4);ts.add(s5); for(Student s:ts) { System.out.println(s.getName()+","+s.getSum()+","+s.getChinese()+","+s.getMath()); } } }