集合多层嵌套使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cnd2449294059/article/details/81699205
package com.cnd06;

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

/**
 * 集合多层嵌套代码体现
 * 
 * @author ndci
 *
 */
public class HashDemo {
	public static void main(String[] args) {
		HashMap<String, HashMap<String, ArrayList<Student>>> czbkMap = new HashMap<String, HashMap<String, ArrayList<Student>>>();

		// czbk北京校区
		HashMap<String, ArrayList<Student>> bjMap = new HashMap<String, ArrayList<Student>>();
		// czbk北京校区->基础班
		ArrayList<Student> array1 = new ArrayList<Student>();
		Student s1 = new Student("李月", 20);
		Student s2 = new Student("赵毅", 22);
		array1.add(s1);
		array1.add(s2);
		bjMap.put("基础班", array1);
		// czbk北京校区->就业班
		ArrayList<Student> array2 = new ArrayList<Student>();
		Student s3 = new Student("钱龙", 21);
		Student s4 = new Student("王军", 24);
		array2.add(s3);
		array2.add(s4);
		bjMap.put("就业班", array2);
		czbkMap.put("北京校区", bjMap);

		// czbk上海校区
		HashMap<String, ArrayList<Student>> shMap = new HashMap<String, ArrayList<Student>>();
		// czbk上海校区->基础班
		ArrayList<Student> array3 = new ArrayList<Student>();
		Student s5 = new Student("李玉龙", 25);
		Student s6 = new Student("王昭君", 26);
		array3.add(s5);
		array3.add(s6);
		shMap.put("基础班", array3);
		// czbk上海校区->就业班
		ArrayList<Student> array4 = new ArrayList<Student>();
		Student s7 = new Student("唐静", 25);
		Student s8 = new Student("朱张", 27);
		array4.add(s7);
		array4.add(s8);
		shMap.put("就业班", array4);
		czbkMap.put("上海校区", shMap);

		// 遍历集合
		Set<String> czbkMapSet = czbkMap.keySet();
		for (String key : czbkMapSet) {
			System.out.println(key);
			HashMap<String, ArrayList<Student>> czbkMapValue = czbkMap.get(key);
			Set<String> czbkMapValueSet=czbkMapValue.keySet();
			for(String key2:czbkMapValueSet) {
				System.out.println("\t"+key2);
				ArrayList<Student> arraylist=czbkMapValue.get(key2);
				for(Student s:arraylist) {
					System.out.println("\t\t"+s.getName()+"-----"+s.getAge());
				}
			}

		}
		
		

	}

}

猜你喜欢

转载自blog.csdn.net/cnd2449294059/article/details/81699205