今天研究了一下用迭代器写的一个嵌套HashMap,写了一个小时。。。脑袋不好使呀
public static void main(String[] args) {
HashMap<Student, String> hm88 = new HashMap<Student, String>();
hm88.put(new Student("haha", 10), "广州");
hm88.put(new Student("kaka", 18), "上海");
hm88.put(new Student("hehe", 15), "深圳");
HashMap<Student, String> hm99 = new HashMap<Student, String>();
hm88.put(new Student("wowo", 10), "广州");
hm88.put(new Student("aoao", 18), "上海");
hm88.put(new Student("bebe", 15), "深圳");
HashMap<HashMap<Student, String>, String> dy = new HashMap<HashMap<Student, String>, String>();
dy.put(hm88, "第88期双元课堂");
dy.put(hm99, "第99期双元课堂");
Set<Map.Entry<HashMap<Student, String>, String>> entry = dy.entrySet();
Iterator<Map.Entry<HashMap<Student, String>, String>> it1 = entry
.iterator();
while (it1.hasNext()) {
Map.Entry<HashMap<Student, String>, String> m1 = it1.next();
String value1 = dy.get(m1.getKey());
HashMap<Student, String> h1 = m1.getKey();
Set<Map.Entry<Student, String>> entry2 = h1.entrySet();
Iterator<Map.Entry<Student, String>> it2 = entry2.iterator();
while (it2.hasNext()) {
Map.Entry<Student, String> m2 = it2.next();
String value2 = m2.getValue();
Student st = m2.getKey();
System.out.println(st + " " + value2 + " " + value1);
}
}
}
/*
* for (HashMap<Student,String> a : dy.keySet()) { String s1=dy.get(a); for
* (Student b : a.keySet()) { String s2=a.get(b);
* System.out.println(b+" "+s2+" "+s1); } }
*/
} |