public int hashCode(){
return name.hashCode()+ age*13;
}
public boolean equals(Object obj){
if (!(obj instanceof Student))
throw new RuntimeException();
Student stu = (Student) obj;
return this.name.equals(stu.name)&& this.age==stu.age && this.score==stu.score;
}
public int compareTo(Student stu){
if (this.getScore() > stu.getScore())
return -1;
if(this.score== stu.score){
stu.getName().compareTo(this.getName());
}
return 1;
}
}
class MyComparator implements Comparator<Student>{
public int compare(Student o1, Student o2){
int n = new Integer(o1.getAge()).compareTo(new Integer(o2.getAge()));
if(n==0){
return new Integer(o1.getScore()).compareTo(new Integer(o2.getScore()));
}
return n;
}
}
public class Test10 {
public static void main(String[] args) {
TreeMap<Student, Integer> mp = new TreeMap<Student, Integer>(new MyComparator());
mp.put(new Student("张三",17), 89);
mp.put(new Student("李四",18), 90);
mp.put(new Student("马六",18), 97);
mp.put(new Student("王五",17), 91);
//第一种取出方式:keySet
/*
Set<Student> se = mp.keySet();
Iterator<Student> it = se.iterator();
while (it.hasNext()){
Student stu =it.next();
int val = mp.get(stu);
System.out.println(stu +",成绩: "+ val);
}
*/
//第二种取出方式:Map.Entry
Set<Map.Entry<Student, Integer>> s1 = mp.entrySet();
Iterator<Map.Entry<Student, Integer>> ti = s1.iterator();
while(ti.hasNext()){
Map.Entry<Student, Integer> m1 =ti.next();
Student stu = m1.getKey();
int val= m1.getValue();
System.out.println(stu +",成绩: "+ val);
}
int n = new Integer(o1.getAge()).compareTo(new Integer(o2.getAge()));
if(n==0){
return new Integer(o1.getScore()).compareTo(new Integer(o2.getScore()));
}
return n;
}
}