集合多层嵌套使用

不念不忘少年蓝@ 2022-05-16 03:20 323阅读 0赞
  1. package com.cnd06;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.Set;
  5. /**
  6. * 集合多层嵌套代码体现
  7. *
  8. * @author ndci
  9. *
  10. */
  11. public class HashDemo {
  12. public static void main(String[] args) {
  13. HashMap<String, HashMap<String, ArrayList<Student>>> czbkMap = new HashMap<String, HashMap<String, ArrayList<Student>>>();
  14. // czbk北京校区
  15. HashMap<String, ArrayList<Student>> bjMap = new HashMap<String, ArrayList<Student>>();
  16. // czbk北京校区->基础班
  17. ArrayList<Student> array1 = new ArrayList<Student>();
  18. Student s1 = new Student("李月", 20);
  19. Student s2 = new Student("赵毅", 22);
  20. array1.add(s1);
  21. array1.add(s2);
  22. bjMap.put("基础班", array1);
  23. // czbk北京校区->就业班
  24. ArrayList<Student> array2 = new ArrayList<Student>();
  25. Student s3 = new Student("钱龙", 21);
  26. Student s4 = new Student("王军", 24);
  27. array2.add(s3);
  28. array2.add(s4);
  29. bjMap.put("就业班", array2);
  30. czbkMap.put("北京校区", bjMap);
  31. // czbk上海校区
  32. HashMap<String, ArrayList<Student>> shMap = new HashMap<String, ArrayList<Student>>();
  33. // czbk上海校区->基础班
  34. ArrayList<Student> array3 = new ArrayList<Student>();
  35. Student s5 = new Student("李玉龙", 25);
  36. Student s6 = new Student("王昭君", 26);
  37. array3.add(s5);
  38. array3.add(s6);
  39. shMap.put("基础班", array3);
  40. // czbk上海校区->就业班
  41. ArrayList<Student> array4 = new ArrayList<Student>();
  42. Student s7 = new Student("唐静", 25);
  43. Student s8 = new Student("朱张", 27);
  44. array4.add(s7);
  45. array4.add(s8);
  46. shMap.put("就业班", array4);
  47. czbkMap.put("上海校区", shMap);
  48. // 遍历集合
  49. Set<String> czbkMapSet = czbkMap.keySet();
  50. for (String key : czbkMapSet) {
  51. System.out.println(key);
  52. HashMap<String, ArrayList<Student>> czbkMapValue = czbkMap.get(key);
  53. Set<String> czbkMapValueSet=czbkMapValue.keySet();
  54. for(String key2:czbkMapValueSet) {
  55. System.out.println("\t"+key2);
  56. ArrayList<Student> arraylist=czbkMapValue.get(key2);
  57. for(Student s:arraylist) {
  58. System.out.println("\t\t"+s.getName()+"-----"+s.getAge());
  59. }
  60. }
  61. }
  62. }
  63. }

发表评论

表情:
评论列表 (有 0 条评论,323人围观)

还没有评论,来说两句吧...

相关阅读

    相关 table中嵌套table

    因工作业务需求,需要展示一个多层嵌套的数据结构,现整理如下,分享给需要的朋友,应用框架vue2。  初始的数据结构如下: new Vue({     el