集合多层嵌套分析

传智播客
	bj 北京校区
		jc 基础班
			aa 20
			bb 21
		jy 就业班
			cc 24
			dd 35
	sh 上海校区
		jc 基础班
			ee 34
			ff 45
		jy 就业班
			gg 36
			hh 44
	gz 广州校区
		jc 基础班
			ii 34
			jj 56
		jy 就业班
			kk 24
			ll 35
	xa 西安校区
		jc 基础班
			mm 36
			nn 40
		jy 就业班
			oo 46
			pp 28
public class test {
	public static void main(String[] args) {
		//创建大集合
		HashMap<String,HashMap<String,ArrayList<Student>>> hm=new HashMap<String,HashMap<String,ArrayList<Student>>>();
		
		//北京校区
		HashMap<String,ArrayList<Student>> h1=new HashMap<String,ArrayList<Student>>();
		//基础班
		ArrayList<Student> a1=new ArrayList<Student>();
		Student s1=new Student("aa",20);
		Student s2=new Student("bb",21);
		//添加学生
		a1.add(s1);
		a1.add(s2);
		//就业班
		ArrayList<Student> a2=new ArrayList<Student>();
		Student s3=new Student("cc",24);
		Student s4=new Student("dd",35);
		//添加学生
		a2.add(s3);
		a2.add(s4);
		h1.put("jc", a1);
		h1.put("jy", a2);
		
		//上海校区
		HashMap<String,ArrayList<Student>> h2=new HashMap<String,ArrayList<Student>>();
		//基础班
		ArrayList<Student> a3=new ArrayList<Student>();
		Student s5=new Student("ee",20);
		Student s6=new Student("ff",21);
		//添加学生
		a3.add(s5);
		a3.add(s6);
		//就业班
		ArrayList<Student> a4=new ArrayList<Student>();
		Student s7=new Student("gg",24);
		Student s8=new Student("hh",35);
		//添加学生
		a4.add(s7);
		a4.add(s8);
		h2.put("jc", a3);
		h2.put("jy", a4);
		
		//广州校区
		HashMap<String,ArrayList<Student>> h3=new HashMap<String,ArrayList<Student>>();
		//基础班
		ArrayList<Student> a5=new ArrayList<Student>();
		Student s9=new Student("ii",20);
		Student s10=new Student("jj",21);
		//添加学生
		a5.add(s9);
		a5.add(s10);
		//就业班
		ArrayList<Student> a6=new ArrayList<Student>();
		Student s11=new Student("kk",24);
		Student s12=new Student("ll",35);
		//添加学生
		a6.add(s11);
		a6.add(s12);
		h3.put("jc", a5);
		h3.put("jy", a6);
		
		//西安校区
		HashMap<String,ArrayList<Student>> h4=new HashMap<String,ArrayList<Student>>();
		//基础班
		ArrayList<Student> a7=new ArrayList<Student>();
		Student s13=new Student("ii",20);
		Student s14=new Student("jj",21);
		//添加学生
		a7.add(s9);
		a7.add(s10);
		//就业班
		ArrayList<Student> a8=new ArrayList<Student>();
		Student s15=new Student("kk",24);
		Student s16=new Student("ll",35);
		//添加学生
		a8.add(s15);
		a8.add(s16);		
		h4.put("jc", a7);
		h4.put("jy", a8);
		
		//添加所有的校区
		hm.put("bj", h1);
		hm.put("sh", h2);
		hm.put("gz", h3);
		hm.put("xa", h4);
		
		//遍历
		Set<String> set=hm.keySet();
		for(String s:set){
			System.out.println("\t"+s);
			HashMap<String,ArrayList<Student>> ss=hm.get(s);
				Set<String> sss=ss.keySet();
					for(String key:sss){
						System.out.println("\t"+"\t"+key);
						ArrayList<Student> list=ss.get(key);
							for(Student st:list){
								System.out.println("\t"+"\t"+"\t"+st.getName()+"---"+st.getAge());
							}
					}
		}
	}
}

结果:

	gz
		jc
			ii---20
			jj---21
		jy
			kk---24
			ll---35
	sh
		jc
			ee---20
			ff---21
		jy
			gg---24
			hh---35
	bj
		jc
			aa---20
			bb---21
		jy
			cc---24
			dd---35
	xa
		jc
			ii---20
			jj---21
		jy
			kk---24
			ll---35
发布了188 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Ting1king/article/details/104795340