黑马程序员技术交流社区
标题:
Map集合的遍历问题
[打印本页]
作者:
sunriselzz
时间:
2013-8-19 21:48
标题:
Map集合的遍历问题
在实际开发中,会使用到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);
System.out.println("-------------第1种遍历方式-----------------");
/*
* Set<String> schoolSet = company.keySet(); Iterator<String>
* schoolSetIt = schoolSet.iterator();
*
* while(schoolSetIt.hasNext()){ String schoolKey = schoolSetIt.next();
* System.out.println(schoolKey);
*
* LinkedHashMap<String,ArrayList<String>> collegeMapValue =
* company.get(schoolKey); Set<String> collegeSet =
* collegeMapValue.keySet(); Iterator<String> collegeIt =
* collegeSet.iterator();
*
* while(collegeIt.hasNext()){ String collegeKey = collegeIt.next();
* System.out.println("\t"+collegeKey);
*
* ArrayList<String> classValueSet = college.get(collegeKey);
* Iterator<String> classIt = classValueSet.iterator();
*
* while(classIt.hasNext()){ String classValue = classIt.next();
* System.out.println("\t\t"+classValue); } } }
*/
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, 下载次数: 23)
下载附件
2013-8-19 21:48 上传
作者:
肥猫
时间:
2013-8-19 22:20
应该是可以的,不过你这样写写的估计够呛,以后要注意写代码不要老一陀陀的全整在主函数里啊,封装在方法内可读性比较好,然后就是你说的连续嵌套,可以用递归的方法嵌套下去,所以建议你将要嵌套的方法封装到单独的方法内方便递归。
作者:
sunriselzz
时间:
2013-8-19 22:27
嗯啊,受教了,我以为遍历的次数不多,没有想到用递归唉!所以当时就呛得晕了,写不下去了.
这个代码是当初学习的时候写的,没有进行完善,还有就是粘上去的时候,没有想到是这种效果.
汗{:soso_e140:}
作者:
神之梦
时间:
2013-8-19 23:48
楼主写这个估计写了很久{:soso_e179:}
作者:
funneies
时间:
2013-8-20 00:48
加了一种遍历方式,代码看着能短点
package test;
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);
System.out.println("-------------第1种遍历方式-----------------");
/*
* Set<String> schoolSet = company.keySet(); Iterator<String>
* schoolSetIt = schoolSet.iterator();
*
* while(schoolSetIt.hasNext()){ String schoolKey = schoolSetIt.next();
* System.out.println(schoolKey);
*
* LinkedHashMap<String,ArrayList<String>> collegeMapValue =
* company.get(schoolKey); Set<String> collegeSet =
* collegeMapValue.keySet(); Iterator<String> collegeIt =
* collegeSet.iterator();
*
* while(collegeIt.hasNext()){ String collegeKey = collegeIt.next();
* System.out.println("\t"+collegeKey);
*
* ArrayList<String> classValueSet = college.get(collegeKey);
* Iterator<String> classIt = classValueSet.iterator();
*
* while(classIt.hasNext()){ String classValue = classIt.next();
* System.out.println("\t\t"+classValue); } } }
*/
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);
}
}
}*/
System.out.println("-------------第3种遍历方式-----------------");
for(Map.Entry<String, LinkedHashMap<String, ArrayList<String>>> mapEntry: company.entrySet()){
Map.Entry<String, LinkedHashMap<String, ArrayList<String>>> entry = mapEntry;
String schoolName = entry.getKey().toString();
System.out.println(schoolName);
LinkedHashMap<String, ArrayList<String>> collegeValueMap = entry.getValue();
for(Map.Entry<String, ArrayList<String>> mapEntry1: collegeValueMap.entrySet()){
Map.Entry<String, ArrayList<String>> entry1 = mapEntry1;
String collegeName = entry1.getKey().toString();
System.out.println("\t" + collegeName);
ArrayList<String> classNameSet = entry1.getValue();
for(String list: classNameSet){
String clazz = list;
System.out.println("\t\t" + clazz);
}
}
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2