- import java.util.*;
- class Student implements Comparable<Student>
- {
- private String name;
- private int age;
- Student(String name,int age)
- {
- this.name=name;
- this.age=age;
- }
- public int compareTo(Student s)
- {
- int num=new Integer(this.age).compareTo(new Integer(s.age));
- if(num==0)
- return this.name.compareTo(s.name);
- return num;
- }
- public String getName()
- {
- return name;
- }
- public int getAge()
- {
- return age;
- }
- public String toString()
- {
- return name+":"+age;
- }
- }
- class Demo3
- {
- public static void main(String[] args)
- {
- HashMap<Student,String> hs=new HashMap<Student,String>();
- hs.put(new Student("zhangsan",23),"beijing");
- hs.put(new Student("zhangsan",23),"tianjin");
- hs.put(new Student("lisi",24),"tianjin");
- hs.put(new Student("wangwu",21),"chongqing");
- hs.put(new Student("liusan",25),"sichuan");
- Set<Student> stu=hs.keySet();
- Iterator<Student> it=stu.iterator();
- while(it.hasNext())
- {
- Student s=it.next();
- String addr=hs.get(s);
- System.out.println(s+"..."+addr);
- }
- }
- }
复制代码
|
|