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());
}
}
}
}
}
三层HashMap的嵌套
最新推荐文章于 2022-09-28 23:28:45 发布