双层集合遍历

import java.util.HashMap;
import java.util.Map;

public class Test {
	public static void main(String[] args) {
		HashMap<Integer, String> javaMap=new HashMap<>();
		javaMap.put(001, "赵一");
		javaMap.put(002, "钱二");
		javaMap.put(003, "孙三");
		javaMap.put(004, "李四");
		javaMap.put(005, "周五");
		javaMap.put(006, "吴六");
		
		HashMap<Integer, String> webMap=new HashMap<>();
		webMap.put(101, "郑七");
		webMap.put(102, "王八");
		webMap.put(103, "冯九");
		webMap.put(104, "陈十");
		webMap.put(105, "褚十一");
		webMap.put(106, "卫十二");
		
		HashMap<String, HashMap<Integer, String>> classMap=new HashMap<String, HashMap<Integer, String>>();
		classMap.put("java", javaMap);
		classMap.put("web",webMap);
		
		//遍历班级classMap集合
		for(Map.Entry<String, HashMap<Integer, String>> mapentry : classMap.entrySet()) {
			System.out.println("班级:"+mapentry.getKey());
			//通过遍历出的班级集合,进一步遍历学生集合
			for(Map.Entry<Integer, String> entry : mapentry.getValue().entrySet()) {
				System.out.println("学号:"+entry.getKey()+"  \t"+"姓名:"+entry.getValue());
			}
		}
	}	
}

猜你喜欢

转载自blog.csdn.net/qq_24801365/article/details/82831160