思路:
1、定义集合
2、存储数据
3、添加元素
4、遍历
4.1将需要遍历的集合的键封装到set集合中(这用到了entrySet方法,和Entry对象)
4.2声明迭代器或者用for增强循环
4.3通过set集合的getKey和getValue方法拿到值和键
5、打印输出就好
代码呈现如下:
1 package com.aaa.demo; 2 3 import java.util.HashMap; 4 import java.util.Iterator; 5 import java.util.Map.Entry; 6 import java.util.Set; 7 8 public class MaoMapDemo { 9 public static void main(String[] args) { 10 //定义Java班的集合 11 HashMap<String, String> javas=new HashMap<String,String>(); 12 //定义Hdoop班的集合 13 HashMap<String, String> Hdoop=new HashMap<String,String>(); 14 //向班级存储学生 15 javas.put("001", "大冰"); 16 javas.put("002", "北岛"); 17 18 Hdoop.put("001", "舒婷"); 19 Hdoop.put("002", "食指"); 20 //定义aaa容器,键是班级的名字 值是两个班级的容器 21 HashMap<String, HashMap<String, String>> aaa=new HashMap<String,HashMap<String,String>>(); 22 aaa.put("现代", javas); 23 aaa.put("近代", Hdoop); 24 entrySet1(aaa); 25 } 26 public static void entrySet(HashMap<String,HashMap<String,String>> aaa){ 27 //调用集合aaa的方法 entrySet将aaa集合的键封装到Set集合中 28 Set<Entry<String,HashMap<String,String>>> classNameSet=aaa.entrySet(); 29 //迭代Set集合 30 Iterator<Entry<String,HashMap<String,String>>> it=classNameSet.iterator(); 31 while(it.hasNext()){ 32 Entry<String,HashMap<String,String>> next=it.next(); 33 //getKey getValue 得到键和值 34 String classNameKey = next.getKey(); 35 System.out.println(classNameKey); 36 //aaa容器的键 班级姓名classMap 37 HashMap<String,String> classMap=next.getValue(); 38 //entrySet将classMap集合的键封装到set集合中 39 Set<Entry<String,String>> studentSet=classMap.entrySet(); 40 //迭代 41 Iterator<Entry<String, String>> studentIt = studentSet.iterator(); 42 while(studentIt.hasNext()){ 43 //遍历集合 44 Entry<String,String> studentEntry=studentIt.next(); 45 String numKey = studentEntry.getKey(); 46 String nameValue = studentEntry.getValue(); 47 System.out.println(numKey+": "+nameValue); 48 } 49 } 50 } 51 public static void entrySet1(HashMap<String,HashMap<String,String>> aaa){ 52 //调用集合aaa的方法 entrySet将aaa集合的键封装到Set集合中 53 Set<Entry<String,HashMap<String,String>>> cNS=aaa.entrySet(); 54 //for循环遍历得到班级 55 for(Entry<String,HashMap<String,String>> next:cNS){ 56 //getKey getValue 得到键和值 57 String classKey = next.getKey(); 58 //aaa容器的键 班级姓名classMap 59 HashMap<String, String> classValue = next.getValue(); 60 //entrySet将classMap集合的键封装到set集合中 61 Set<Entry<String, String>> studentSet = classValue.entrySet(); 62 System.out.println(classKey); 63 //for循环遍历 64 //Set<Entry<String, String>> entrySet = classValue.entrySet(); 65 for(Entry<String, String> studentEntry:studentSet){ 66 String numKey = studentEntry.getKey(); 67 String nameValue = studentEntry.getValue(); 68 System.out.println(numKey+": "+nameValue); 69 } 70 } 71 } 72 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律