学 Java的第30天 Map中嵌套Map
package cn.aaa;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.HashMap;
import java.util.Set;
//Map中嵌套存储Map
//aaa
//java班
// 001 周一
// 002 周二
//hoodp班
// 001 周三
// 002 周四
//
//java班 :存学号和名字
//hoodp班:存学号和名字
//aaa:存的是班级
// java班<学号,姓名>
// hoodp班<学号,姓名>
// aaaMap<班级名字,java班>
public class Map {
public static void main(String[] args) {
HashMap<Integer, String> java=new HashMap<Integer, String>();
HashMap<Integer, String> hoodp=new HashMap<Integer, String>();
java.put(001, "周一");
java.put(002, "周二");
hoodp.put(001, "周三");
hoodp.put(002, "周四");
HashMap<String, HashMap<Integer, String>> aaa=new HashMap<String, HashMap<Integer, String>>();
aaa.put("java班", java);
aaa.put("hoodp班", hoodp);
// entrySet(aaa);
fun(aaa);
}
//增强for循环
public static void fun(HashMap<String, HashMap<Integer, String>> aaa) {
Set<Entry<String, HashMap<Integer, String>>> classNameSet=aaa.entrySet();
for (Entry<String, HashMap<Integer, String>> entry : classNameSet) {
String classNameKey=entry.getKey();
System.out.println(classNameKey);
HashMap<Integer, String> classMap=entry.getValue();
// System.out.println(classMap);
Set<Entry<Integer, String>> studentSet=classMap.entrySet();
for (Entry<Integer, String> entry2 : studentSet) {
int numKey=entry2.getKey();
String nameValue=entry2.getValue();
System.out.println(numKey+":"+nameValue);
}
}
}
//迭代
public static void entrySet(HashMap<String, HashMap<Integer, String>> aaa) {
Set<Entry<String, HashMap<Integer, String>>> classNameSet=aaa.entrySet();
Iterator<Entry<String, HashMap<Integer, String>>> it=classNameSet.iterator();
while(it.hasNext()){
Entry<String, HashMap<Integer, String>> next=it.next();
String classNameKey=next.getKey();
HashMap<Integer, String> classMap=next.getValue();
System.out.println(classNameKey);
// System.out.println(classMap);
Set<Entry<Integer, String>> studentSet=classMap.entrySet();
Iterator<Entry<Integer, String>> studentIt=studentSet.iterator();
while(studentIt.hasNext()){
Entry<Integer,String> studentEntry=studentIt.next();
int numKey=studentEntry.getKey();
String nameValue=studentEntry.getValue();
System.out.println(numKey+":"+nameValue);
}
}
}
}