黑马程序员技术交流社区
标题:
Map集合中的多重嵌套遍历问题
[打印本页]
作者:
sunriselzz
时间:
2013-8-19 23:03
标题:
Map集合中的多重嵌套遍历问题
本帖最后由 sunriselzz 于 2013-8-31 23:07 编辑
在实际开发中,会使用到Map集合的多重嵌套遍历么?
如果重数更多(嵌套遍历>3),那程序代码就显得复杂了,阅读性差,请问这时候还是使用这种遍历么?
如例如,本人在学习Map集合时写了个HashMap集合的3重嵌套实现传智播客公司校区,及学院,学科班级的遍历
但是,如果我还要再得到它们每个班级的学员信息呢?那我还能再继续这样嵌套遍历下去么?
package cn.genericdefine;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
public class HashMapCZBK {
/**
* HashMap集合3重嵌套实现传智播客公司校区,及学院,学科班级的遍历
*
* 传智播客公司 |--北京校区 |--Java学院 |--.Net学院 |--PHP学院 |--平面设计学院 |--IOS学院
*
* |--上海校区 |--Java学院 |--.Net学院 |--PHP学院 |--平面设计学院
*
* |--广州校区 |--Java学院 |--.Net学院 |--PHP学院
*
* |--成都校区 |--Java学院 |--.Net学院
*
* @param args
*/
public static void main(String[] args) {
// 创建公司集合company
LinkedHashMap<String, LinkedHashMap<String, ArrayList<String>>> company =
new LinkedHashMap<String, LinkedHashMap<String, ArrayList<String>>>();
// 创建班级集合
ArrayList<String> javaClass = new ArrayList<String>();
javaClass.add("JavaSE基础班");
javaClass.add("JavaEE就业班");
javaClass.add("Android就业班");
ArrayList<String> netClass = new ArrayList<String>();
netClass.add(".NET基础班");
netClass.add(".NET就业班");
ArrayList<String> phpClass = new ArrayList<String>();
phpClass.add("PHP基础班");
phpClass.add("PHP就业班");
ArrayList<String> iosClass = new ArrayList<String>();
iosClass.add("IOS基础班");
iosClass.add("IOS就业班");
ArrayList<String> designClass = new ArrayList<String>();
designClass.add("平面设计基础班");
designClass.add("平面设计就业班");
// 创建学院集合
LinkedHashMap<String, ArrayList<String>> college =
new LinkedHashMap<String, ArrayList<String>>();
college.put("Java学院", javaClass);
college.put(".Net学院", netClass);
college.put("PHP学院", phpClass);
college.put("IOS学院", iosClass);
college.put("平面设计学院", designClass);
// 创建校区集合
company.put("北京校区", college);
company.put("上海校区", college);
company.put("广州校区", college);
company.put("成都校区", college);
printCZBK(company);
}
public static void printCZBK(LinkedHashMap<String, LinkedHashMap<String,ArrayList<String>>> company) {
System.out.println("-------------第2种遍历方式-----------------");
Set<Map.Entry<String, LinkedHashMap<String, ArrayList<String>>>> schoolSet = company.entrySet();
Iterator<Map.Entry<String, LinkedHashMap<String, ArrayList<String>>>> schoolIt = schoolSet.iterator();
while (schoolIt.hasNext()) {
Map.Entry<String, LinkedHashMap<String, ArrayList<String>>> schoolme = schoolIt.next();
String schoolName = schoolme.getKey();
System.out.println(schoolName);
LinkedHashMap<String, ArrayList<String>> collegeValueMap = schoolme.getValue();
Set<Map.Entry<String, ArrayList<String>>> collegeSet = collegeValueMap.entrySet();
Iterator<Map.Entry<String, ArrayList<String>>> collegeIt = collegeSet.iterator();
while (collegeIt.hasNext()) {
Map.Entry<String, ArrayList<String>> collegeme = collegeIt.next();
String collegeName = collegeme.getKey();
System.out.println("\t" + collegeName);
ArrayList<String> classNameSet = collegeme.getValue();
Iterator<String> classIt = classNameSet.iterator();
while (classIt.hasNext()) {
String clazz = classIt.next();
System.out.println("\t\t" + clazz);
}
}
}
}
}
复制代码
运行效果总分截图:
效果图.jpg
(604.74 KB, 下载次数: 81)
下载附件
2013-8-19 23:02 上传
作者:
范龙波
时间:
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