java对vector动态数组中的对象排序,以下代码有何问题,如何修改?
package com.tx.collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
public class Student {
String name;
int score;
public Student(String n, int s) {
name = n;
score = s;
}
public boolean equals(Object o) {
Student other = (Student) o;
return other.score == score;
}
public String toString() {
return name + "," + score;
}
public static void main(String[] args) {
Vector<Student> stu = new Vector<Student>();
stu.add(new Student("张三", 90));
stu.add(new Student("李四", 80));
stu.add(new Student("小明", 85));
stu.add(new Student("王五", 60));
stu.add(new Student("何六", 70));
Student index = new Student("", 60);
int t = stu.indexOf(index);
System.out.println("成绩为60的学生姓名为" + stu.elementAt(t).name);
Set<Student> set = new TreeSet(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
if (o1.score > o2.score)
return -1;
else if (o1.score < o2.score)
return 1;
else
return 0;
}
});
set.addAll(stu);
Iterator<Student> iterator = set.iterator();
while(iterator.hasNext()){
Student s = iterator.next();
System.out.println(s.name+":"+s.score);
}
}
}
2017-09-29
~
~
~