Map的嵌套 练习
Map的嵌套 练习
利用迭代和增强for循环的两种方式实现如下效果
package cn.ccc;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
public class two {
public static void main(String[] args) {
//定义java班的集合
HashMap<String, String> java = new HashMap<String, String>();
//向班级存储学生
java.put("20190322", "第一名");
java.put("20190323", "第二名");
java.put("20190324", "第三名");
//定义Dhoop班的集合
HashMap<String, String> Dhoop = new HashMap<String,String>();
//向Dhoop班级存储学生
Dhoop.put("20190401", "第一名");
Dhoop.put("20190402", "第二名");
Dhoop.put("20190403", "第三名");
//定义集合aa容器 键是班级的名字 值是两个班级的容器
HashMap<String, HashMap<String, String>> aa = new HashMap<String,HashMap<String, String>>();
aa.put("java班", java);
aa.put("Dhoop班", Dhoop);
EntrySet(aa);
}
private static void EntrySet(HashMap<String, HashMap<String, String>> aa) {
//调用集合aa的方法entrySet将aa集合的键封装到Set集合中
Set<Entry<String, HashMap<String, String>>> classenset = aa.entrySet();
//迭代Set集合
Iterator<Entry<String, HashMap<String, String>>> it = classenset.iterator();
while(it.hasNext()){
Entry<String, HashMap<String, String>> classnext = it.next();
String classkey = classnext.getKey();
HashMap<String, String> classvalue = classnext.getValue();
System.out.println(classkey);
///
Set<Entry<String, String>> studentset = classvalue.entrySet();
Iterator<Entry<String, String>> studentit = studentset.iterator();
while(studentit.hasNext()){
Entry<String, String> studentnext = studentit.next();
String numk = studentnext.getKey();
String numv = studentnext.getValue();
System.out.println(numk+" "+numv);
}
}
System.out.println(".........................................................");
//增强for循环
Set<Entry<String, HashMap<String, String>>> forclassset = aa.entrySet();
for(Entry<String, HashMap<String, String>> i:forclassset){
String classk = i.getKey();
HashMap<String, String> classv = i.getValue();
Set<Entry<String, String>> forstudentset = classv.entrySet();
System.out.println(classk);
for(Entry<String, String> j:forstudentset){
String numkey = j.getKey();
String numvalue = j.getValue();
System.out.println(numkey+" "+numvalue);
}
}
}
}
最新文章
- 小菜学习Winform(五)窗体间传递数据
- 你能熟练使用Dictionary字典和List列表吗?(转)
- Leetcode: Non-overlapping Intervals
- MySQL和PHP基础考试错题回顾
- mysql sql 百万级数据库优化方案
- Android沉浸式(侵入式)标题栏(状态栏)Status(三)
- IEEE 802.11 标准列表
- hdu 3790 最短路径问题(两个限制条件的最短路)
- 分布式java应用
- C#中Thread.Join()的理解
- FOJ 2170 花生的序列 dp
- 关于web程序中使用KindEditor向数据库插入带有格式的数据时出现的问题
- HDU 2298 Toxophily(公式/三分+二分)
- 学习安卓开发[4] - 使用隐式Intent启动短信、联系人、相机应用
- pip安装软件或模块时提示cannot import name &#39;main&#39;
- Win10 for Docker 安装 K8S
- web.xml中的ContextLoaderListener和DispatcherServlet区别
- BZOJ.3784.树上的路径(点分治 贪心 堆)
- 【BZOJ】3142: [Hnoi2013]数列
- jmeter线程组介绍