import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
class Student
{
private String name ;
private int age ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
public class Demo {
public static void main(String[]args)
{
HashMap<String,Student> keyMap = new HashMap<>();
keyMap.put("第一个",new Student("番茄",21));
keyMap.put("第二个",new Student("黑白",23));
keyMap.put("第三个",new Student("蓝蓝",22));
//获取每一个键数组.
Set<String> key = keyMap.keySet();
//遍历键数组.转成键字符
Iterator<String> it = key.iterator();
//获取键.
while(it.hasNext()){
String keys = it.next();
Student stu = keyMap.get(keys);
System.out.println(keys+";"+stu.getName()+";"+stu.getAge());
/* for(String s:key){
Student stu = keyMap.get(key);
System.out.println(s+"---"+stu.getName()+stu.getAge());*/
// }
}
}
}
|
|