package org.westos.HashMap集合嵌套的遍历;
import java.util.HashMap;
import java.util.Set;
/**
*HashMap集合嵌套HashMap集合;
*HashMap集合作为键时
**/
public class Text1 {
public static void main(String[] args) {
HashMap<HashMap<String,Integer>,String> map = new
HashMap<HashMap<String,Integer>,String>();
HashMap<String,Integer> hm1 = new HashMap<>();
hm1.put("杨过", 23);
hm1.put("小龙女", 33);
map.put(hm1, "神雕侠侣");
HashMap<String,Integer> hm2 = new HashMap<>();
hm2.put("张无忌", 23);
hm2.put("赵敏", 33);
map.put(hm2, "倚天屠龙记");
//set集合存储map集合的键
Set<HashMap<String,Integer>> set = map.keySet();
for(HashMap<String,Integer> hm: set) {
System.out.println(map.get(hm));
//set集合存储map集合键的键
Set<String> setkey = hm.keySet();
for(String str:setkey) {
int i = hm.get(str);
System.out.println("\t"+str+"----"+i);
}
}
}
}
package org.westos.HashMap集合嵌套的遍历;
import java.util.HashMap;
import java.util.Set;
/**
* HashMap集合嵌套遍历
* HashMap集合嵌套HashMap集合
* HashMap集合作为值时
**/
public class Text2 {
public static void main(String[] args) {
HashMap<String,HashMap<String,Integer>> map = new HashMap<>();
HashMap<String,Integer> hm1 = new HashMap<>();
hm1.put("杨过", 23);
hm1.put("小龙女", 33);
map.put("神雕侠侣", hm1);
HashMap<String,Integer> hm2 = new HashMap<>();
hm2.put("张无忌", 23);
hm2.put("赵敏", 33);
map.put("倚天屠龙记", hm2);
//set集合存储map集合的键
Set<String> set = map.keySet();
for(String s:set) {
HashMap<String,Integer> hm = map.get(s);
System.out.println(s);
//set集合存储map集合值的键
Set<String> setvelunkey = hm.keySet();
for(String str:setvelunkey) {
int i = hm.get(str);
System.out.println("\t"+str+"----"+i);
}
}
}
}