- import java.util.*;
- public class MapDemo1 {
- public static void main(String[] args)
- {
- Map<Student, String> stuMap = new TreeMap<Student, String>(new Comp());
- stuMap.put(new Student("胡平燕1", 21),"湖南1");
- stuMap.put(new Student("胡平燕2", 23),"湖南2");
- stuMap.put(new Student("胡平燕3", 50),"湖南4");
- stuMap.put(new Student("胡平燕3", 29),"湖南4");
- stuMap.put(new Student("胡平燕3", 29),"湖南3");
-
-
- Set<Student> keySet = stuMap.keySet();
- for(Iterator<Student> it = keySet.iterator(); it.hasNext();)
- {
- Student key = it.next();
- String name= stuMap.get(key);
- System.out.println(key.getName()+" "+key.getAge()+" "+name);
- }
- }
- }
- class Comp implements Comparator<Student>
- {
- @Override
- public int compare(Student o1, Student o2)
- {
- int num = o1.getName().compareTo(o2.getName());
- if (num == 0)
- {
- return new Integer(o1.getAge()).compareTo(new Integer(o2.getAge()));
- }
- return -1;
- }
- }
- class Student
- {
- private String name;
- private int age;
- Student(String name,int age)
- {
- this.name = name;
- this.age = age;
- }
- public int getAge()
- {
- return age;
- }
- public String getName()
- {
- return name;
- }
- }
复制代码 输出的结果是这样滴:
胡平燕3 29 湖南3
胡平燕3 50 湖南4
胡平燕2 23 湖南2
胡平燕1 21 null
什么原因呢 ??
|
|