集合框架_集合多层嵌套的代码体现

package cn.itcast_06;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

/*
 * 为了更符合要求:
 * 		这次的数据就看成是学生对象。
 * 
 * 北大青鸟
 * 		bj	北京校区
 * 			jc	基础班
 * 					林青霞	27
 * 					风清扬	30
 * 			jy	就业班	
 * 					赵雅芝	28
 * 					李刚		29
 * 		sh	上海校区
 * 			jc	基础班
 * 					郭美美	20
 * 					犀利哥	22
 * 			jy	就业班	
 * 					罗玉凤	21
 * 					马征		23
 * 		gz	广州校区
 * 			jc	基础班
 * 					王力宏	30
 * 					李静磊	32
 * 			jy	就业班	
 * 					郎朗		31
 * 					柳岩		33
 * 		xa	西安校区
 * 			jc	基础班
 * 					范冰冰	27
 * 					孙成文	30
 * 			jy	就业班	
 * 					李冰冰	28
 * 					张志豪	29
 */
public class HashMapDemo {
	public static void main(String[] args) {
		// 创建大集合
		HashMap>> bdqnMap = new HashMap>>();

		// 北京校区
		HashMap> bjBdqnMap = new HashMap>();
		ArrayList bj_jcArray = new ArrayList();
		Student bj_s1 = new Student("林青霞", 27);
		Student bj_s2 = new Student("风清扬", 30);
		bj_jcArray.add(bj_s1);
		bj_jcArray.add(bj_s2);
		bjBdqnMap.put("基础班", bj_jcArray);
		ArrayList bj_jyArray = new ArrayList();
		Student bj_s3 = new Student("赵雅芝", 28);
		Student bj_s4 = new Student("李刚", 29);
		bj_jyArray.add(bj_s3);
		bj_jyArray.add(bj_s4);
		bjBdqnMap.put("就业班", bj_jyArray);
		bdqnMap.put("北京校区", bjBdqnMap);

		// 上海校区
		HashMap> shBdqnMap = new HashMap>();
		ArrayList sh_jcArray = new ArrayList();
		Student sh_s1 = new Student("郭美美", 20);
		Student sh_s2 = new Student("犀利哥", 22);
		sh_jcArray.add(sh_s1);
		sh_jcArray.add(sh_s2);
		shBdqnMap.put("基础班", sh_jcArray);
		ArrayList sh_jyArray = new ArrayList();
		Student sh_s3 = new Student("罗玉凤", 21);
		Student sh_s4 = new Student("马征	", 23);
		sh_jyArray.add(sh_s3);
		sh_jyArray.add(sh_s4);
		shBdqnMap.put("就业班", sh_jyArray);
		bdqnMap.put("上海校区", shBdqnMap);

		// 广州校区
		HashMap> gzBdqnMap = new HashMap>();
		ArrayList gz_jcArray = new ArrayList();
		Student gz_s1 = new Student("王力宏", 30);
		Student gz_s2 = new Student("李静磊", 32);
		gz_jcArray.add(gz_s1);
		gz_jcArray.add(gz_s2);
		gzBdqnMap.put("基础班", gz_jcArray);
		ArrayList gz_jyArray = new ArrayList();
		Student gz_s3 = new Student("郎朗", 31);
		Student gz_s4 = new Student("柳岩", 33);
		gz_jyArray.add(gz_s3);
		gz_jyArray.add(gz_s4);
		gzBdqnMap.put("就业班", gz_jyArray);
		bdqnMap.put("广州校区", gzBdqnMap);

		// 西安校区
		HashMap> xaBdqnMap = new HashMap>();
		ArrayList xa_jcArray = new ArrayList();
		Student xa_s1 = new Student("范冰冰", 27);
		Student xa_s2 = new Student("孙成文", 30);
		xa_jcArray.add(xa_s1);
		xa_jcArray.add(xa_s2);
		xaBdqnMap.put("基础班", xa_jcArray);
		ArrayList xa_jyArray = new ArrayList();
		Student xa_s3 = new Student("李冰冰", 28);
		Student xa_s4 = new Student("张志豪", 29);
		xa_jyArray.add(xa_s3);
		xa_jyArray.add(xa_s4);
		xaBdqnMap.put("就业班", xa_jyArray);
		bdqnMap.put("西安校区", xaBdqnMap);

		// 遍历集合
		Set bdqnMapSet = bdqnMap.keySet();
		for (String bdqnMapKey : bdqnMapSet) {// bdqnMapKey校区
			System.out.println(bdqnMapKey + "\t");
			HashMap> bdqnMapValue = bdqnMap
					.get(bdqnMapKey);
			Set bdqnMapValueSet = bdqnMapValue.keySet();
			for (String bdqnMapValueKey : bdqnMapValueSet) {// bdqnMapValueKey班级
				System.out.println("\t" + bdqnMapValueKey + "\t");
				ArrayList array = bdqnMapValue.get(bdqnMapValueKey);
				for (Student stu : array) {// array学生
					System.out.println("\t\t" + stu.getName() + "---"
							+ stu.getAge());
				}
			}
		}
	}
}

你可能感兴趣的:(Java,ArrayList,Map,HashMap)