package cn.cast.MapDemo;
import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;
import cn.cast.Person.Person;
public class MapDemo {
public static void main(String[] args) {
Map<Person, Integer> m = new TreeMap<Person, Integer>(new MyComparator());
m.put(new Person("赵六", 21), 80);
m.put(new Person("王五", 18), 80);
m.put(new Person("赵六", 21), 70);
m.put(new Person("李四", 20), 88);
m.put(new Person("赵六", 20), 60);
m.put(new Person("张三", 19), 85);
for(Person p : m.keySet()) {
System.out.println(p + m.get(p)); //不明白为什么报错 请明白人解释 下为什么 怎么改
}
}
}
class MyComparator implements Comparator<Person> {
public int compare(Person o1, Person o2) {
int ageGap = o1.getAge() - o2.getAge();
int nameGap = o1.getName().compareTo(o2.getName());
return nameGap != 0 ? nameGap : ageGap;
}
}
|