import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
public class TestTreeMap {
public static void main(String[] args) {
new TestTreeMap().test();
}
void test(){
Map map = new TreeMap(new StudentComparator());
map.put(new Student("zhangsan",22), "22");
map.put(new Student("lisi",78), "22");
map.put(new Student("wangwu",12), "22");
System.out.println(map.keySet());
}
class Student {
private String name;
private double score;
public Student(String name,double score){
this.name = name;
this.score = score;
}
public double getScore(){
return this.score;
}
@Override
public String toString() {
return "姓名:"+this.name+",分数:"+this.score;
}
}
class StudentComparator implements Comparator<Student> {
@Override
public int compare(Student o1,Student o2) {
if(o1.getScore() > o2.getScore()){
return 1;
}else if(o1.getScore() < o2.getScore()){
return -1;
}else{
return 0;
}
}
}
}
TreeMap
最新推荐文章于 2024-08-07 23:42:58 发布