**Java 遍历 HashMap 编程
**
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Student {
private String name;
private int id;
public Student(int id, String name) {
super();
this.name = name;
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public static void main(String[] args) {
Student st1 = new Student(0001, “ 张三 ”);
Student st2 = new Student(0002, “ 李四 ”);
Student st3 = new Student(0003, “Javaweb.cc”);
Map<Integer, String> map = new HashMap<Integer, String>();
map.put(st1.getId(), st1.getName());
map.put(st2.getId(), st2.getName());
map.put(st3.getId(), st3.getName());
Set<Integer> set = map.keySet();
Iterator<Integer> it = set.iterator();
while (it.hasNext()) {
int i = it.next();
String str = map.get(i);
System.out.println(“ID: ” + i + “\t” + “ 姓名:” + str);
}
}
}