[img]C:\Users\Administrator.USER-20150306LN\Desktop- package com.Heima;
- import java.util.Comparator;
- import java.util.TreeSet;
- public class TestStudent {
- public static void main(String[] args) {
- TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {
- @Override
- public int compare(com.Heima.Student o1, com.Heima.Student o2) {
- int num =o2.getScore() - o1.getScore() ;
- int nums = o1.getAge() - o2.getAge();
-
- int numst = (num == 0) ? nums : num;
- int result =(nums == 0) ? (o1.getName().compareTo(o2.getName())) : numst;
- return result;
- }
- });
- Student s1 = new Student("Tom",24,89);
- Student s2 = new Student("Robin",32,99);
- Student s3 = new Student("Jerry",24,99);
- Student s4 = new Student("Lili",23,87);
- Student s5 = new Student("Jack",22,87);
- Student s6 = new Student("LiLei",25,95);
- Student s7 = new Student("Robin",32,99);
-
- ts.add(s1);
- ts.add(s2);
- ts.add(s3);
- ts.add(s4);
- ts.add(s5);
- ts.add(s6);
- ts.add(s7);
-
-
- for(Student s : ts ){
- System.out.println(s);
- }
- }
- }
复制代码 [/img] |