java Map练习
描述学生,map容器,学生为键,地址为值,获取map中的元素。 public class MapDemo { public static void main(String[] args) { HashMap<Student, String> hm = new HashMap<Student, String>(); hm.put(new Student("lisi1", 1), "北京"); hm.put(new Student("lisi2", 2), "上海"); hm.put(new Student("lisi3", 3), "天津"); hm.put(new Student("lisi4", 4), "武汉"); //第一种取出方式 keySet Set<Student> keySet = hm.keySet(); Iterator<Student> it = keySet.iterator(); while (it.hasNext()) { Student stu = it.next(); String addr = hm.get(stu); System.out.println(stu + "--" + addr); } //第二种取出方式 entrySet Set<Map.Entry<Student, String>> entrySet = hm.entrySet();