一、HashMap集合 嵌套 HashMap集合:
HashMap<String,HashMap<String,Integer>> gaoyiMap = new HashMap<String,HashMap<String,Integer>>();
//1班名单
HashMap<String,Integer> class1Map = new HashMap<String, Integer>();
class1Map.put("张三",21);
class1Map.put("李四",21);
class1Map.put("王五",20);
class1Map.put("赵六",22);
//2班名单
HashMap<String,Integer> class2Map = new HashMap<String, Integer>();
class2Map.put("张三三",21);
class2Map.put("李四四",21);
class2Map.put("王五五",20);
class2Map.put("赵六六",22);
//往gaoyiMap加入1班和2班名单
gaoyiMap.put("class1",class1Map);
gaoyiMap.put("class2",class2Map);
Set<Map.Entry<String, HashMap<String, Integer>>> entries = gaoyiMap.entrySet();
for (Map.Entry<String,HashMap<String,Integer>> me : entries) {
//gaoyiMap的值还是HashMap类型,所以对值再进行一次entrySet操作
Set<Map.Entry<String, Integer>> entrySet2 = me.getValue().entrySet();
for (Map.Entry<String,Integer> me2 : entrySet2) {
System.out.println(me.getKey()+"——"+me2.getKey()+":"+me2.getValue());
}
}
————————————————————————————
二、HashMap集合 嵌套 ArrayList集合:
HashMap<String, ArrayList<String>> bookMap = new HashMap<String,ArrayList<String>>();
ArrayList<String> book_1 = new ArrayList<String>();
book_1.add("刘备");
book_1.add("关羽");
book_1.add("张飞");
ArrayList<String> book_2 = new ArrayList<String>();
book_2.add("宋江");
book_2.add("武松");
book_2.add("李逵");
ArrayList<String> book_3 = new ArrayList<String>();
book_3.add("孙悟空");
book_3.add("猪八戒");
book_3.add("沙和尚");
bookMap.put("三国演义",book_1);
bookMap.put("水浒传",book_2);
bookMap.put("西游记",book_3);
Set<Map.Entry<String,ArrayList<String>>> entries = bookMap.entrySet();
for (Map.Entry<String,ArrayList<String>> me : entries) {
for (String s : me.getValue()) {
System.out.println(me.getKey()+"-"+s);
}
}
——————————————————————————————————
三、 ArrayList集合 嵌套 HashMap集合:
ArrayList<HashMap<String,Integer>> array = new ArrayList<HashMap<String, Integer>>();
HashMap<String,Integer> h_1 = new HashMap<String, Integer>();
h_1.put("孙悟空",19);
h_1.put("猪八戒",30);
h_1.put("沙和尚",30);
HashMap<String,Integer> h_2 = new HashMap<String, Integer>();
h_2.put("宋江",34);
h_2.put("李逵",29);
h_2.put("武松",28);
array.add(h_1);
array.add(h_2);
for (HashMap<String,Integer> hashMap: array) {
Set<Map.Entry<String,Integer>> entries = hashMap.entrySet();
for (Map.Entry<String,Integer> me : entries) {
System.out.println(me.getKey()+":"+me.getValue());
}
}