JAVA之
Map之
HashMap嵌套HashMap
-------------------------------------------------
HashMap<String, HashMap<String,Integer > >
【HashMap里还有HashMap集合对象】
/*
* 1班---
* 王五 --- 13
* 趙六 --- 19
*
* 2班---
* 張三 --- 20
* 李四 --- 22
*
*/
-------------------------------------------------
package com.lyMapHashMap;
import java.util.HashMap;
import java.util.Set;
/**
* HashMap嵌套HashMap
* @author Jack
*/
/*
* 1班---
* 王五 --- 13
* 趙六 --- 19
*
* 2班---
* 張三 --- 20
* 李四 --- 22
*
*/
public class HashMapDemo {
public static void main(String[] args) {
HashMap<String, HashMap<String, Integer>> hm = new HashMap<String, HashMap<String, Integer>>();
// 1班
HashMap<String, Integer> oneMap = new HashMap<String,Integer>();
// 放入学生对象
oneMap.put("王五", 13);
oneMap.put("趙六", 19);
//加入一班
hm.put("1班", oneMap);
// 2班
HashMap<String, Integer> twoMap = new HashMap<String,Integer>();
// 放入学生对象
twoMap.put("張三", 20);
twoMap.put("李四", 22);
// 加入二班
hm.put("2班", twoMap);
// 准备遍历HashMap集合【双层For循环】
Set<String> hmKey = hm.keySet();
for(String hmKeyKey : hmKey){
System.out.println(hmKeyKey);
HashMap<String, Integer> hmKeyValue = hm.get(hmKeyKey);
Set<String> set = hmKeyValue.keySet();
for(String s : set){
int sValue = hmKeyValue.get(s);
System.out.println("\t"+s +"---"+ sValue);
}
}
}
}
················结果···················
2班
李四---22
張三---20
1班
趙六---19
王五---13
·········································