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);
}
}

}
}

最新文章

  1. 小菜学习Winform(五)窗体间传递数据
  2. 你能熟练使用Dictionary字典和List列表吗?(转)
  3. Leetcode: Non-overlapping Intervals
  4. MySQL和PHP基础考试错题回顾
  5. mysql sql 百万级数据库优化方案
  6. Android沉浸式(侵入式)标题栏(状态栏)Status(三)
  7. IEEE 802.11 标准列表
  8. hdu 3790 最短路径问题(两个限制条件的最短路)
  9. 分布式java应用
  10. C#中Thread.Join()的理解
  11. FOJ 2170 花生的序列 dp
  12. 关于web程序中使用KindEditor向数据库插入带有格式的数据时出现的问题
  13. HDU 2298 Toxophily(公式/三分+二分)
  14. 学习安卓开发[4] - 使用隐式Intent启动短信、联系人、相机应用
  15. pip安装软件或模块时提示cannot import name &#39;main&#39;
  16. Win10 for Docker 安装 K8S
  17. web.xml中的ContextLoaderListener和DispatcherServlet区别
  18. BZOJ.3784.树上的路径(点分治 贪心 堆)
  19. 【BZOJ】3142: [Hnoi2013]数列
  20. jmeter线程组介绍

热门文章

  1. ThinkPHP5.0 开发手册
  2. explode() 字符串分割函数
  3. ereg
  4. Linux中一个文件10行内容,如何输出5-8内容到屏幕
  5. 初学者如何迅速学习web前端开发
  6. 编译libcurl支持https协议
  7. windows2012的服务器远程桌面提示内部错误的问题解决方法
  8. Jsの练习-数组常用方法 -forEach()
  9. Java继承2
  10. Vue语法学习第四课(2)——class与style的绑定