if (this.getAge() > s1.getAge())
return 1;
else if (this.getAge() < s1.getAge()) {
return -1;
} else {
if (this.getScore() > s1.getScore())
return 1;
else if (this.getScore() == s1.getScore())
return this.getName().compareTo(s1.getName());
else if (this.getScore() < s1.getScore())
return -1;
}
return 0;
}
}
public class TestMap1 {
public static void main(String[] args) {
@SuppressWarnings("unchecked")
Map<Integer, Student> hm = new TreeMap();
hm.put(18, new Student(12, 76, "jim"));
hm.put(25, new Student(16, 79, "jim"));
hm.put(85, new Student(54, 76, "jim"));
hm.put(38, new Student(23, 86, "jim"));
hm.put(4, new Student(12, 56, "jim"));
hm.put(3, new Student(45, 96, "jim"));
hm.put(9, new Student(12, 56, "kum"));
hm.put(56, new Student(12, 76, "kumm"));
hm.put(45, new Student(12, 779, "jim"));
Set<Integer> se = hm.keySet();
Set stu = new TreeSet();
for (Iterator it = se.iterator(); it.hasNext();) {
stu.add(hm.get(it.next()));
}
for (Iterator st = stu.iterator(); st.hasNext();) {
System.out.println(st.next());
}
作者: 王思兰 时间: 2012-3-6 13:21
Map<Integer, Student> hm = new TreeMap();
使用泛型的话,最好全部都加上 Map<Integer, Student> hm = new TreeMap<Integer, Student>();
还有后面也是 Set<Integer> se = hm.keySet();
Set<Integer> stu = new TreeSet<Integer>();