• HashMap的嵌套应用


    public class HashMapDemo3 {
    public static void main(String[] args) {
    //
    HashMap<String,HashMap<String,ArrayList<Student>>> czbkMap=new HashMap<String,HashMap<String,ArrayList<Student>>>();
    //北京校区
    HashMap<String,ArrayList<Student>> bjCzbkMap=new HashMap<String,ArrayList<Student>>();
    ArrayList<Student> array1=new ArrayList<Student>();
    Student bs1=new Student("jc张三",20);
    Student bs2=new Student("jc李四",21);
    array1.add(bs1);
    array1.add(bs2);
    ArrayList<Student> array2=new ArrayList<Student>();
    Student bs3=new Student("jy张三",20);
    Student bs4=new Student("jy李四",21);
    array2.add(bs3);
    array2.add(bs4);

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

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

    // System.out.println(czbkMap);
    //上海校区
    HashMap<String,ArrayList<Student>> shCzbkMap=new HashMap<String,ArrayList<Student>>();
    ArrayList<Student> array3=new ArrayList<Student>();
    Student ss1=new Student("jc张三",20);
    Student ss2=new Student("jc李四",21);
    array3.add(ss1);
    array3.add(ss2);
    ArrayList<Student> array4=new ArrayList<Student>();
    Student ss3=new Student("jy张三",20);
    Student ss4=new Student("jy李四",21);
    array4.add(ss3);
    array4.add(ss4);

    shCzbkMap.put("基础班", array3);
    shCzbkMap.put("就业班", array4);

    czbkMap.put("上海校区", shCzbkMap);


    //武汉校区
    HashMap<String,ArrayList<Student>> whCzbkMap=new HashMap<String,ArrayList<Student>>();
    ArrayList<Student> array5=new ArrayList<Student>();
    Student ws1=new Student("jc张三",20);
    Student ws2=new Student("jc李四",21);
    array5.add(ws1);
    array5.add(ws2);
    ArrayList<Student> array6=new ArrayList<Student>();
    Student ws3=new Student("jy张三",20);
    Student ws4=new Student("jy李四",21);
    array6.add(ws3);
    array6.add(ws4);

    whCzbkMap.put("基础班", array1);
    whCzbkMap.put("就业班", array2);

    czbkMap.put("武汉校区", whCzbkMap);


    //广州校区
    HashMap<String,ArrayList<Student>> gzCzbkMap=new HashMap<String,ArrayList<Student>>();
    ArrayList<Student> array7=new ArrayList<Student>();
    Student gs1=new Student("jc张三",20);
    Student gs2=new Student("jc李四",21);
    array7.add(gs1);
    array7.add(gs2);
    ArrayList<Student> array8=new ArrayList<Student>();
    Student gs3=new Student("jy张三",20);
    Student gs4=new Student("jy李四",21);
    array8.add(gs3);
    array8.add(gs4);

    gzCzbkMap.put("基础班", array7);
    gzCzbkMap.put("就业班", array8);

    czbkMap.put("广州校区", gzCzbkMap);

    //遍历集合
    Set<String> czbkMapSet=czbkMap.keySet();

    for(String strKey:czbkMapSet ){
    HashMap<String,ArrayList<Student>> czbkMapSetValue=czbkMap.get(strKey);
    System.out.println(strKey);
    Set<String> czbkMapValueSet=czbkMapSetValue.keySet();
    for(String studentKey:czbkMapValueSet){
    ArrayList<Student> arr=czbkMapSetValue.get(studentKey);
    System.out.println(" "+studentKey);
    for(Student s:arr){
    System.out.println(" "+s.getName()+"--------"+s.getAge());
    }

    }
    }
    }
    }

  • 相关阅读:
    Android 测试工具集01
    Android ImageView的ScaleType属性
    Android Webview与Html5交互
    Java Notes 00
    Android 显示原理简介
    Android开发在路上:少去踩坑,多走捷径
    Android_Dialog cancle 和dismiss 区别
    SpringMVC框架
    Redis持久化
    Redis集群
  • 原文地址:https://www.cnblogs.com/liumin-txgt/p/13403806.html
Copyright © 2020-2023  润新知