废话不多说,直接上代码块!
import java.util.HashMap;
import java.util.Map;
/**
* Map集合嵌套Map集合遍历
* Created by aongi on 2017/4/28.
* Version 1.0
*/
public class MapOf {
public static void bl(HashMap<String, HashMap<String,String>> jd){
for (Map.Entry<String, HashMap<String, String>> me : jd.entrySet()) {
String classNameKey = me.getKey();
HashMap<String, String> numNameMapValue = me.getValue();
for (Map.Entry<String, String> nameMapEntry : numNameMapValue.entrySet()) {
String numKey = nameMapEntry.getKey();
String nameValue = nameMapEntry.getValue();
System.out.println(classNameKey + ".." + numKey + ".." + nameValue);
}
}
}
public static void main(String[] args) {
HashMap<String, String> fuy = new HashMap<String, String>();
HashMap<String, String> cs = new HashMap<String, String>();
fuy.put("服务员001", "小王");
fuy.put("服务员002", "小李");
cs.put("厨师001", "小张");
cs.put("厨师002", "小龙");
HashMap<String, HashMap<String,String>> jd = new HashMap<String, HashMap<String,String>>();
jd.put("服务员", fuy);
jd.put("厨师", cs);
bl(jd);
}
}
还有一种简单的遍历
public static void bl(HashMap<String, HashMap<String,String>> jd){
for(String a:jd.keySet()){
HashMap<String,String> map =jd.get(a);
for(String s:map.keySet()){
String s1= map.get(s);
System.out.println(a+" "+s+" "+s1);
}
}
}