<pre name="code" class="java">package test;
import java.util.HashMap;
import java.util.Iterator;
public class hashmapdemo {
public static void main(String[] args){
HashMap<Student,String> hm = new HashMap<Student,String>();
hm.put(new Student("wangwu",23),"shanghai");
hm.put(new Student("wangwenwen",28),"北京");
hm.put(new Student("zhangming",20),"武汉");
hm.put(new Student("xiaoqiang",35),"天津");
Iterator<Student> it = hm.keySet().iterator();
while(it.hasNext()){
Student key = it.next();
String value = hm.get(key);
System.out.println(key.name+":"+key.age+"----"+value);
}
}
}
同一个包下的
package test;public class Student {int age;String name;public Student(String name,int age){this.name = name;this.age = age;}}