这是我写出来的,就是不知道怎么在Student类里面放多个对象,你可以参考下
public class homework1 {
public static void main(String[] args) {
Student student =new Student();
student.setName("baby");
student.setAge(18);
HashMap<Student ,String> mapping =new HashMap<Student ,String>();
String address = "北京市海淀区";
mapping.put(student, address);
System.out.println(mapping);
Set<Student> keySet = mapping.keySet();
// for (Student thiskeySet : keySet) {
// String thisvalue=mapping.get(thiskeySet);
//
// System.out.println(thiskeySet+"="+thisvalue);
// }
//第二种遍历方法
/*Set<Entry<Student,String>> entrySet = mapping.entrySet();
//迭代集合,依次获取每一个键值对对应关系
for(Entry<Student,String> thisEntry : entrySet) {
//通过键值对对应关系,分别获取键与值
Student key = thisEntry.getKey();
String value = thisEntry.getValue();
System.out.println(key+"="+value);
}*/
}
}
|