黑马程序员技术交流社区
标题:
HashMap嵌套HashMap的四种遍历方法<手打>
[打印本页]
作者:
一脸的护胸毛丶
时间:
2015-11-20 01:16
标题:
HashMap嵌套HashMap的四种遍历方法<手打>
package com.heima.hashmap;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
import com.heima.bean.Student;
public class Test2_HashMapHashMap {
/**
* @param args
*/
public static void main(String[] args) {
//定义88期基础班
HashMap<Student, String> hm88 = new HashMap<>();
hm88.put(new Student("丁一",21), "济南");
hm88.put(new Student("毛二",22), "德州");
//定义99期基础班
HashMap<Student, String> hm99 = new HashMap<>();
hm99.put(new Student("张三",23), "北京");
hm99.put(new Student("赵四",24), "大连");
//定义双元课堂
HashMap<HashMap<Student, String>, String> hm = new HashMap<>();
hm.put(hm88, "双元88期基础班");
hm.put(hm99, "双元99期基础班");
/*//keySet迭代器方法遍历
Set<HashMap<Student, String>> keySet = hm.keySet(); //班级为键的集合
Iterator<HashMap<Student, String>> it = keySet.iterator(); //
while(it.hasNext()) {
HashMap<Student, String> key = it.next();
String className = hm.get(key);
Set<Student> keySet1 = key.keySet();
Iterator<Student> itt = keySet1.iterator();
while(itt.hasNext()) {
Student student = itt.next();
String adress = key.get(student);
System.out.println("学生:" + student + ",籍贯:" + adress + ",班级:" + className);
}
}*/
/*//keySet for循环遍历
for(HashMap<Student, String> one : hm.keySet()) {
String classname = hm.get(one);
for(Student stu : one.keySet()) {
String adress = one.get(stu);
System.out.println("学生:" + stu + ",籍贯:" + adress + ",班级:" + classname);
}
}*/
/*//entrySet迭代器遍历
Set<Entry<HashMap<Student, String>, String>> set = hm.entrySet(); //
Iterator<Entry<HashMap<Student, String>, String>> it = set.iterator();
while(it.hasNext()) {
Entry<HashMap<Student, String>, String> en = it.next();
String className = en.getValue();
System.out.println(className+"有以下学生:");
HashMap<Student, String> subHm = en.getKey();
Set<Entry<Student, String>> subSet = subHm.entrySet();
Iterator<Entry<Student, String>> subIt = subSet.iterator();
while(subIt.hasNext()) {
Entry<Student, String> subEntry = subIt.next();
System.out.println(subEntry.getKey()+":"+subEntry.getValue());
}
}*/
//entrySet增强for循环
Set<Entry<HashMap<Student, String>, String>> set = hm.entrySet();
for(Entry<HashMap<Student, String>, String> entry : set) {
String className = entry.getValue();
HashMap<Student, String> key = entry.getKey();
Set<Entry<Student, String>> subSet = key.entrySet();
for(Entry<Student, String> subEntry : subSet) {
String city = subEntry.getValue();
Student student = subEntry.getKey();
System.out.println("姓名:" + student + ",籍贯:" + city + ",班级:" + className);
}
}
}
}
作者:
ln0491
时间:
2015-11-20 09:07
学习了。。。。。。。。。。
作者:
qq331335030
时间:
2015-11-20 09:56
大牛啊,无限崇拜,疯狂学习中......
作者:
一脸的护胸毛丶
时间:
2015-11-20 21:51
ln0491 发表于 2015-11-20 09:07
学习了。。。。。。。。。。
学习了还不加点分~~~
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2