黑马程序员技术交流社区

标题: Map集合中的多重嵌套遍历问题 [打印本页]

作者: sunriselzz    时间: 2013-8-19 23:03
标题: Map集合中的多重嵌套遍历问题
本帖最后由 sunriselzz 于 2013-8-31 23:07 编辑

在实际开发中,会使用到Map集合的多重嵌套遍历么?
如果重数更多(嵌套遍历>3),那程序代码就显得复杂了,阅读性差,请问这时候还是使用这种遍历么?
如例如,本人在学习Map集合时写了个HashMap集合的3重嵌套实现传智播客公司校区,及学院,学科班级的遍历
但是,如果我还要再得到它们每个班级的学员信息呢?那我还能再继续这样嵌套遍历下去么?
  1. package cn.genericdefine;

  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.LinkedHashMap;
  5. import java.util.Map;
  6. import java.util.Set;

  7. public class HashMapCZBK {

  8.         /**
  9.          * HashMap集合3重嵌套实现传智播客公司校区,及学院,学科班级的遍历
  10.          *
  11.          * 传智播客公司 |--北京校区 |--Java学院 |--.Net学院 |--PHP学院 |--平面设计学院 |--IOS学院
  12.          *
  13.          * |--上海校区 |--Java学院 |--.Net学院 |--PHP学院 |--平面设计学院
  14.          *
  15.          * |--广州校区 |--Java学院 |--.Net学院 |--PHP学院
  16.          *
  17.          * |--成都校区 |--Java学院 |--.Net学院
  18.          *
  19.          * @param args
  20.          */
  21.         public static void main(String[] args) {
  22.                 // 创建公司集合company
  23.                 LinkedHashMap<String, LinkedHashMap<String, ArrayList<String>>> company =
  24.                         new LinkedHashMap<String, LinkedHashMap<String, ArrayList<String>>>();

  25.                 // 创建班级集合
  26.                 ArrayList<String> javaClass = new ArrayList<String>();
  27.                 javaClass.add("JavaSE基础班");
  28.                 javaClass.add("JavaEE就业班");
  29.                 javaClass.add("Android就业班");

  30.                 ArrayList<String> netClass = new ArrayList<String>();
  31.                 netClass.add(".NET基础班");
  32.                 netClass.add(".NET就业班");

  33.                 ArrayList<String> phpClass = new ArrayList<String>();
  34.                 phpClass.add("PHP基础班");
  35.                 phpClass.add("PHP就业班");

  36.                 ArrayList<String> iosClass = new ArrayList<String>();
  37.                 iosClass.add("IOS基础班");
  38.                 iosClass.add("IOS就业班");

  39.                 ArrayList<String> designClass = new ArrayList<String>();
  40.                 designClass.add("平面设计基础班");
  41.                 designClass.add("平面设计就业班");

  42.                 // 创建学院集合
  43.                 LinkedHashMap<String, ArrayList<String>> college =
  44.                         new LinkedHashMap<String, ArrayList<String>>();

  45.                 college.put("Java学院", javaClass);
  46.                 college.put(".Net学院", netClass);
  47.                 college.put("PHP学院", phpClass);
  48.                 college.put("IOS学院", iosClass);
  49.                 college.put("平面设计学院", designClass);

  50.                 // 创建校区集合
  51.                 company.put("北京校区", college);
  52.                 company.put("上海校区", college);
  53.                 company.put("广州校区", college);
  54.                 company.put("成都校区", college);

  55.                 printCZBK(company);
  56.                
  57.         }

  58.         public static void printCZBK(LinkedHashMap<String, LinkedHashMap<String,ArrayList<String>>> company) {
  59.                
  60.                 System.out.println("-------------第2种遍历方式-----------------");
  61.                 Set<Map.Entry<String, LinkedHashMap<String, ArrayList<String>>>> schoolSet = company.entrySet();
  62.                 Iterator<Map.Entry<String, LinkedHashMap<String, ArrayList<String>>>> schoolIt = schoolSet.iterator();

  63.                 while (schoolIt.hasNext()) {
  64.                         Map.Entry<String, LinkedHashMap<String, ArrayList<String>>> schoolme = schoolIt.next();
  65.                         String schoolName = schoolme.getKey();
  66.                         System.out.println(schoolName);
  67.                         
  68.                         LinkedHashMap<String, ArrayList<String>> collegeValueMap = schoolme.getValue();

  69.                         Set<Map.Entry<String, ArrayList<String>>> collegeSet = collegeValueMap.entrySet();
  70.                         Iterator<Map.Entry<String, ArrayList<String>>> collegeIt = collegeSet.iterator();

  71.                         while (collegeIt.hasNext()) {
  72.                                 Map.Entry<String, ArrayList<String>> collegeme = collegeIt.next();
  73.                                 String collegeName = collegeme.getKey();
  74.                                 System.out.println("\t" + collegeName);

  75.                                 ArrayList<String> classNameSet = collegeme.getValue();
  76.                                 Iterator<String> classIt = classNameSet.iterator();
  77.                                 while (classIt.hasNext()) {
  78.                                         String clazz = classIt.next();
  79.                                         System.out.println("\t\t" + clazz);
  80.                                 }

  81.                         }

  82.                 }
  83.         }

  84. }
复制代码
运行效果总分截图:




效果图.jpg (604.74 KB, 下载次数: 81)

效果图.jpg

作者: 范龙波    时间: 2013-8-28 23:13
应该会用到,但要考虑嵌套层数,
其实和for一样的道理 ,两层for常见 ,但3层,4层就少见了很多,为什么呢? 主要还是性能问题.如果是4层for那么执行的次数就是4的n次方
不过我之前到写过一个4层for, 但要优化 ,使用break,continue 优化完也可以做到2的n次的  但不好的地方是可读性很差
作者: 以防万一    时间: 2013-8-31 14:30
{:soso_e176:}
亲,请问下问题是否解决?
如果已解决请及时将未解决改为已解决
如果未解决请回帖追问
三天未回复的将视为已解决

详情参考: 如何更改分类

保持队形,谢谢合作{:soso_e121:}

作者: 以防万一    时间: 2013-8-31 21:42
老李,,,看了帖子也不说改了分类{:soso_e138:}
作者: sunriselzz    时间: 2013-8-31 23:11
杨璐敏 发表于 2013-8-31 21:42
老李,,,看了帖子也不说改了分类

完了,完了,哥隐姓埋名这么多年,结果还是被你这么用心地发现了,晕菜,你也太用心了吧!{:soso_e183:}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2