java中的HashMap多层嵌套
作者:jingxian 发布时间:2023-11-27 07:34:52
标签:java,HashMap,多层嵌套
java HashMap多层嵌套
package chapter12;
import java.util.HashMap;
public class Demo03 {
//班级 -组 -学生
static HashMap<String,HashMap<String,HashMap<String,Integer>>> map= new HashMap<>();
public static void main(String[] args) {
HashMap<String,HashMap<String,Integer>> map50= new HashMap<>();
HashMap<String,Integer> map50_1= new HashMap<>();
map50_1.put("50_1_3",77);
map50_1.put("50_1_2",88);
map50_1.put("50_1_1",99);
map50.put("第一组",map50_1);
HashMap<String,Integer> map50_2= new HashMap<>();
map50_2.put("50_2_3",75);
map50_2.put("50_2_2",85);
map50_2.put("50_2_1",95);
map50.put("第二组",map50_2);
map.put("通达50班",map50);
HashMap<String,HashMap<String,Integer>> map51= new HashMap<>();
HashMap<String,Integer> map51_1= new HashMap<>();
map51_1.put("51_1_3",77);
map51_1.put("51_1_2",88);
map51_1.put("51_1_1",99);
map51.put("第一组",map51_1);
HashMap<String,Integer> map51_2= new HashMap<>();
map51_2.put("51_2_3",75);
map51_2.put("51_2_2",85);
map51_2.put("51_2_1",95);
map51.put("第二组",map51_2);
map.put("通达51班",map51);
HashMap<String,HashMap<String,Integer>> map52= new HashMap<>();
HashMap<String,Integer> map52_1= new HashMap<>();
map52_1.put("52_1_3",77);
map52_1.put("52_1_2",88);
map52_1.put("52_1_1",99);
map52.put("第一组",map52_1);
HashMap<String,Integer> map52_2= new HashMap<>();
map52_2.put("52_2_3",75);
map52_2.put("52_2_2",85);
map52_2.put("52_2_1",95);
map52.put("第二组",map52_2);
map.put("通达52班",map52);
map.forEach((className,cMap)->{
System.out.println(className+"信息展示:");
System.out.println("*******************************");
cMap.forEach((group,gMap)->{
System.out.println(className+group);
gMap.forEach((name,score)->{
System.out.println("姓名:"+name+"\t分数:"+score);
});
});
System.out.println("*****************************");
});
// forEach();
}
// public static void forEach(){
// map.forEach((className,cMap)->{
// System.out.println(className+"信息展示:");
// System.out.println("*******************************");
// cMap.forEach((group,gMap)->{
// System.out.println(className+group);
// gMap.forEach((name,score)->{
// System.out.println("姓名:"+name+"\t分数:"+score);
// });
// });
// System.out.println("*****************************");
// });
// }
}
三层HashMap的嵌套
package cn.itcast_04;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
/*
* 三层HashMap的嵌套
*
* 动漫小说
* dm 动漫区
* rm 日漫:
* 漩涡鸣人 27
* 宇智波佐助 30
* gm 国漫:
* 闻人翊 29
* 张楚岚 29
* xs 小说区
* xh 玄幻:
* 狠人大帝 25
* 林动 32
* qh 奇幻
* 唐三 21
* 比比东 42
*/
public class 集合的多层嵌套 {
public static void main(String[] args) {
//创建集合1
HashMap<String,HashMap<String,ArrayList<Student>>> dx =
new HashMap<String,HashMap<String,ArrayList<Student>>>();
//创建动漫区
HashMap<String,ArrayList<Student>> dm = new HashMap<String,ArrayList<Student>> ();
//创建日漫集合
ArrayList<Student> array1 = new ArrayList<Student>();
//创建并添加数据
Student s1 = new Student("漩涡鸣人 ",27);
Student s2 = new Student("宇智波佐助 ",30);
array1.add(s1);
array1.add(s2);
//创建国漫集合
ArrayList<Student> array2 = new ArrayList<Student>();
//创建并添加数据
Student s3 = new Student("闻人翊",29);
Student s4 = new Student("张楚岚 ",29);
array2.add(s3);
array2.add(s4);
//添加
dm.put("日漫", array1);
dm.put("国漫", array2);
//创建小说区
HashMap<String,ArrayList<Student>> xs = new HashMap<String,ArrayList<Student>>();
//创建玄幻集合
ArrayList<Student> array3 = new ArrayList<Student>();
//创建并添加数据
Student s5 = new Student("狠人大帝 ",25);
Student s6 = new Student("林动 ",32);
array3.add(s5);
array3.add(s6);
//创建奇幻集合
ArrayList<Student> array4 = new ArrayList<Student>();
//创建并添加数据
Student s7 = new Student("唐三 ",21);
Student s8 = new Student("比比东 ",42);
array4.add(s7);
array4.add(s8);
//添加
xs.put("玄幻", array3);
xs.put("奇幻", array4);
dx.put("动漫", dm);
dx.put("小说", xs);
//遍历
Set<String> hmset= dx.keySet();
for(String hmkey:hmset) {
System.out.println(hmset);
HashMap<String,ArrayList<Student>> hmvalue = dx.get(hmkey);
Set<String> hm1set= hmvalue.keySet();
for(String hm1key:hm1set) {
System.out.println("\t"+hm1set);
ArrayList<Student> value = hmvalue.get(hm1key);
for(Student s:value) {
System.out.println("\t\t"+s.getName()+"-------"+s.getAng());
}
}
}
}
}
来源:https://blog.csdn.net/qq_57420582/article/details/125625776


猜你喜欢
- 接下来想做一个图廊,所以并没有必要用立方体,只需做一些“墙壁”就行了。而在一个平面上建起另一个矩形的
- 1 运算符1.1 概述运算符 用于连接 表达式 的 操作数,并对操作数执行运算。例如,表达式num1+num2,其操作数是num1和num2
- 项目介绍医院管理系统,分为管理员、医生、病人三种角色;管理员主要功能包括:首页、系统管理:医生管理、患者管理、药品管理;预约管理;病史管理;
- Java的SPI机制实例详解SPI的全名为Service Provider Interface.普通开发人员可能不熟悉,因为这个是针对厂商或
- 不论你是否关注,Java Web应用都或多或少的使用了线程池来处理请求。线程池的实现细节可能会被忽视,但是有关于线程池的使用和调优迟早是需要
- 目录前沿快速开始引入依赖定义接口配置类开始调用json序列化接口层面指定header:指定Encoder跟Decoder使用 * 注解详解@
- 在上一篇Android RecylerView入门教程中提到,RecyclerView不再负责Item视图的布局及显示,所以Recycler
- 目录Update计时器Invoke协程DoTween最开始接触unity的时候,我做延时都是在update里做计时器。后来才发现,我屮艸芔茻
- 这个是常见的对二叉树的操作。总结一下:设节点的数据结构,如下:class TreeNode { char
- 本文实例为大家分享了C#生成验证码图片的具体代码,供大家参考,具体内容如下/// <summary>
- 新版的IDEA为了防止 pom 更新时,MAVEN 自动导包时卡死的问题,取消了自动导包机制。但新增了导入按钮和快捷键。 问题id
- 引言思考:HashTable是线程安全的,为什么不推荐使用?HashTable是一个线程安全的类,它使用synchronized来锁住整张H
- 今天上班中午吃饱之后、逛博客溜达看到一道题:数组反转 晚上回家洗完澡没事情做,就自己练习一把。public static cla
- 标题index界面加载问题刚开始学习springBoot记录一下遇到的小问题1.index.html加载不出来的问题我习惯性的将index.
- 今天工作中遇到一个需求,就是获取 excel 里面的内容,并且把 excel 另存为 csv,因为本人以前未接触过,所以下面整理出来的代码均
- 基本流foreach 实现了外部迭代,它是建立于集合的基础上;stream 流实现的是内部迭代中间操作与终端操作对任意集合使用 stream
- 一、设计模式概述设计模式,是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他
- 写在前面并发编程一直都存在,只不过过去的很长时间里,比较难以实现,随着互联网的发展,人口红利的释放,更加友好的支持并发编程已经成了主流编程语
- < drawable name="white">#FFFFFF< /drawable><
- 无论是我们在使用word还是记事本,系统都会为我们提供撤销的功能,这几乎是人人都会使用到的功能,而在我们实际开发中,会不会存在一个很复杂的对