- import java.util.* ;
- class MapTest
- {
- public static void main(String[] args)
- {
- HashMap<Student,String> hp = new HashMap<Student,String>();
- hp.put(new Student("LP01",15),"bj");
- hp.put(new Student("LP02",15),"sh");
- hp.put(new Student("LP03",15),"nj");
- hp.put(new Student("LP03",15),"tj");
- hp.put(new Student("LP04",15),"hah");
-
- Set<Map.Entry<Student,String>> entrySet = hp.entrySet();
- for (Iterator<Map.Entry<Student,String>> iter =entrySet.iterator();iter.hasNext() ; )
- {
- Map.Entry<Student,String> me = iter.next();
- Student s = me.getKey();
- String ads = me.getValue();
- System.out.println(s+"________"+ads);
- }
- }
- }
- class Student implements Comparable<Student>
- {
- private String name;
- private int age ;
- Student(String name ,int age)
- {
- this.name = name ;
- this.age = age ;
- }
- public String getName()
- {
- return name ;
- }
- public int getAge()
- {
- return age ;
- }
- public int hashCode()
- {
- return name.hashCode()+age*13 ;
- }
- public boolean equals(Object obj)
- {
- if (!(obj instanceof Student))
- throw new ClassCastException("该对象不是学生");
- Student stu =(Student)obj;
- return this.name.equals(stu.name)&&this.age==stu.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 ;
- }
- }
复制代码
|
|