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
0
投稿
猜你喜欢
- * 其实就是java.lang.reflect.Proxy类动态的根据您指定的所有接口生成一个class byte,该class会继承P
- ViewDragHelper是support.v4下提供的用于处理拖拽滑动的辅助类,查看Android的DrawerLayout源码,可以发
- 一、获取android工程里面的各种资源的id; 1.1 string型 比如下面: << string name=”OK”&g
- javax.el.ELException的解决方式failed to parse the expression [${xxx}]Tomcat
- @Value注解读取yml中的map配置网上查了好多资料,都是.properties文件中读取,而且又是几个人抄来抄去,找了半天功夫不负有心
- spring-mybatis获取mapper方式汇总项目背景:pojo下面有一个user实体类Dao包下面写了usermapper.xml
- 本文实例讲述了Android基于SoftReference缓存图片的方法。分享给大家供大家参考,具体如下:Java中的SoftReferen
- Java线程分为两类分别为daemon线程(守护线程)和User线程(用户线程),在JVM启动时候会调用main函数,main函数所在的线程
- 突然心血来潮,想自己做个小程序玩玩,但是怎么把他做成一个exe文件,让大家能够更好的理解和使用呢,百度了一下,说是需要exe4j来生成,但是
- 工厂方法模式(Factory Method):定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到其子类。工
- strcpy函数详解如下1.函数介绍1.1.函数接口char * __cdecl strcpy(char * dst, const char
- 前言在原生的 Android 或 iOS 中,都提供了基本的键值对存储方式,Android 是 SharedPreferences,iOS
- 起源flutter作为一个跨平台的框架,在绘制上体现出了它跨平台的良好性能.那么,它是如何从runApp()后 绘制上屏的呢?本文将与你一起
- 什么是Spring BootSpring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初
- package com.test; import java.io.BufferedReader; import jav
- 自定义工具类PropertyUtil,并在该类的static静态代码块中读取properties文件内容保存在static属性中以供别的程序
- 引言一个复杂的分布式系统,用户发起一个请求,这个请求可能调用几十到几百个服务,经过很多业务层,而每个业务又是多个机器集群,一个请求具体被随机
- 1.背景在java语言中还没有引入枚举类型之前,表示枚举类型的常用模式是声明一组具有int常量。之前我们通常利用public final s
- 本教程适合新手小白,Java7之前的版本是没有内置JavaFx的,Java7-10是内置JavaFx的,但是到了Java10以后的版本,Or
- 本文实例讲述了Android中TextView显示插入的图片实现方法。分享给大家供大家参考,具体如下:Android系统默认给TextVie