HashMap嵌套HashMap嵌套HashMap(三层嵌套)

HashMap嵌套HashMap嵌套HashMap(三层嵌套)

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 {

         publicstatic void main(String[] args) {

                   //创建大集合

                   HashMap>> czbkMap = newHashMap>>();

 

                   //北京校区数据

                   HashMap> bjCzbkMap = new HashMap>();

                   ArrayListarray1 = new ArrayList();

                   Students1 = new Student("林青霞", 27);

                   Students2 = new Student("风清扬", 30);

                   array1.add(s1);

                   array1.add(s2);

                   ArrayListarray2 = new ArrayList();

                   Students3 = new Student("赵雅芝", 28);

                   Students4 = new Student("武鑫", 29);

                   array2.add(s3);

                   array2.add(s4);

                   bjCzbkMap.put("基础班",array1);

                   bjCzbkMap.put("就业班",array2);

                   czbkMap.put("北京校区",bjCzbkMap);

 

                   //晚上可以自己练习一下

                   //上海校区数据自己做

                   //广州校区数据自己做

 

                   //西安校区数据

                   HashMap> xaCzbkMap = new HashMap>();

                   ArrayListarray3 = new ArrayList();

                   Students5 = new Student("范冰冰", 27);

                   Students6 = new Student("刘意", 30);

                   array3.add(s5);

                   array3.add(s6);

                   ArrayListarray4 = new ArrayList();

                   Students7 = new Student("李冰冰", 28);

                   Students8 = new Student("张志豪", 29);

                   array4.add(s7);

                   array4.add(s8);

                   xaCzbkMap.put("基础班",array3);

                   xaCzbkMap.put("就业班",array4);

                   czbkMap.put("西安校区",xaCzbkMap);

 

                   //遍历集合

                   SetczbkMapSet = czbkMap.keySet();

                   for(String czbkMapKey : czbkMapSet) {

                            System.out.println(czbkMapKey);

                            HashMap> czbkMapValue = czbkMap

                                               .get(czbkMapKey);

                            SetczbkMapValueSet = czbkMapValue.keySet();

                            for(String czbkMapValueKey : czbkMapValueSet) {

                                     System.out.println("\t"+ czbkMapValueKey);

                                     ArrayListczbkMapValueValue = czbkMapValue

                                                        .get(czbkMapValueKey);

                                     for(Student s : czbkMapValueValue) {

                                               System.out.println("\t\t"+ s.getName() + "---"

                                                                 +s.getAge());

                                     }

                            }

                   }

         }

}

运行结果:

西安校区

    就业班

        李冰冰---28

        张志豪---29

    基础班

        范冰冰---27

        刘意---30

北京校区

    就业班

        赵雅芝---28

        武鑫---29

    基础班

        林青霞---27

        风清扬---30

 

你可能感兴趣的:(java,集合)