展开全部
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 stu = new Vector();
stu.add(new Student("张三", 90));
stu.add(new Student("李四", 80));
stu.add(new Student("小明32313133353236313431303231363533e58685e5aeb931333363386231", 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 set = new TreeSet(new Comparator() {
@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 iterator = set.iterator();
while(iterator.hasNext()){
Student s = iterator.next();
System.out.println(s.name+":"+s.score);
}
}
}