import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
class Student implements Comparable {
private int age;
private int score;
private String name;
Student(int age, int score, String name) {
this.name = name;
this.age = age;
this.score = score;
}
public int getAge() {
return age;
}
public int getScore() {
return score;
}
public String getName() {
return name;
}
public String toString() {
return this.age + "....." + this.score + "======" + this.name;
}
public int compareTo(Object o1) {
Student s1 = (Student) o1;
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());
}
|
|