java中的HashMap多層嵌套
更新時(shí)間:2022年09月29日 09:53:20 投稿:jingxian
這篇文章主要介紹了java中的HashMap多層嵌套問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
java HashMap多層嵌套


package chapter12;
import java.util.HashMap;
public class Demo03 {
//班級 -組 -學(xué)生
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("通達(dá)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("通達(dá)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("通達(dá)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分?jǐn)?shù):"+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分?jǐn)?shù):"+score);
// });
// });
// System.out.println("*****************************");
// });
// }
}三層HashMap的嵌套
package cn.itcast_04;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
/*
* 三層HashMap的嵌套
*
* 動漫小說
* dm 動漫區(qū)
* rm 日漫:
* 漩渦鳴人 27
* 宇智波佐助 30
* gm 國漫:
* 聞人翊 29
* 張楚嵐 29
* xs 小說區(qū)
* xh 玄幻:
* 狠人大帝 25
* 林動 32
* qh 奇幻
* 唐三 21
* 比比東 42
*/
public class 集合的多層嵌套 {
public static void main(String[] args) {
//創(chuàng)建集合1
HashMap<String,HashMap<String,ArrayList<Student>>> dx =
new HashMap<String,HashMap<String,ArrayList<Student>>>();
//創(chuàng)建動漫區(qū)
HashMap<String,ArrayList<Student>> dm = new HashMap<String,ArrayList<Student>> ();
//創(chuàng)建日漫集合
ArrayList<Student> array1 = new ArrayList<Student>();
//創(chuàng)建并添加數(shù)據(jù)
Student s1 = new Student("漩渦鳴人 ",27);
Student s2 = new Student("宇智波佐助 ",30);
array1.add(s1);
array1.add(s2);
//創(chuàng)建國漫集合
ArrayList<Student> array2 = new ArrayList<Student>();
//創(chuàng)建并添加數(shù)據(jù)
Student s3 = new Student("聞人翊",29);
Student s4 = new Student("張楚嵐 ",29);
array2.add(s3);
array2.add(s4);
//添加
dm.put("日漫", array1);
dm.put("國漫", array2);
//創(chuàng)建小說區(qū)
HashMap<String,ArrayList<Student>> xs = new HashMap<String,ArrayList<Student>>();
//創(chuàng)建玄幻集合
ArrayList<Student> array3 = new ArrayList<Student>();
//創(chuàng)建并添加數(shù)據(jù)
Student s5 = new Student("狠人大帝 ",25);
Student s6 = new Student("林動 ",32);
array3.add(s5);
array3.add(s6);
//創(chuàng)建奇幻集合
ArrayList<Student> array4 = new ArrayList<Student>();
//創(chuàng)建并添加數(shù)據(jù)
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());
}
}
}
}
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中@Autowired與@Resource注解的區(qū)別詳解
這篇文章主要介紹了Java中@Autowired與@Resource注解的區(qū)別詳解,@Resource的作用相當(dāng)于@Autowired,只不過@Autowired按byType自動注入,而@Resource默認(rèn)按 byName自動注入罷了,@Resource有兩個(gè)屬性是比較重要的,需要的朋友可以參考下2023-11-11
idea社區(qū)版如何設(shè)置vm?options
這篇文章主要介紹了idea社區(qū)版如何設(shè)置vm?options問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09
解決idea中svn提交時(shí)performing vcs refresh時(shí)間很長的問題
這篇文章主要介紹了解決idea中svn提交時(shí)performing vcs refresh時(shí)間很長的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
datax-web在windows環(huán)境idea中模塊化打包部署操作步驟
這篇文章主要介紹了datax-web在windows環(huán)境idea中模塊化打包部署操作步驟,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05
spring整合JMS實(shí)現(xiàn)同步收發(fā)消息(基于ActiveMQ的實(shí)現(xiàn))
本篇文章主要介紹了spring整合JMS實(shí)現(xiàn)同步收發(fā)消息(基于ActiveMQ的實(shí)現(xiàn)),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
淺談Java中Unicode的編碼和實(shí)現(xiàn)
這篇文章向大家介紹了Java編程中Unicode編碼及實(shí)現(xiàn)的相關(guān)內(nèi)容,列舉了幾個(gè)字符不同表達(dá)式的比較,以及Unicode平面映射的知識,具有一點(diǎn)點(diǎn)參考價(jià)值,需要的朋友可以了解下。2017-10-10
spring使用JavaConfig進(jìn)行配置的方法
這篇文章主要介紹了spring使用JavaConfig進(jìn)行配置的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-09-09

