HashMap<String,HashMap<String,Integer>> hm = new HashMap<String,HashMap<String,Integer>>();
HashMap<String,Integer> hm1 = new HashMap<String,Integer>();
hm1.put("曹操",25);
hm1.put("周瑜",20);
HashMap<String,Integer> hm2 = new HashMap<String,Integer>();
hm2.put("贾宝玉",21);
hm2.put("林黛玉",18);
hm.put("三国",hm1);
hm.put("红楼梦",hm2);
//1.得到键的集合hm1 hm2
Set<String> keyset1 = hm.keySet();
//2.遍历键的集合
for(String key1 : keyset1){
//3.得到键值对对象
HashMap<String,Integer> keyvalue = hm.get(key1);
//4.得到键的集合曹操 周瑜 贾宝玉 林黛玉
Set<String> keyset2 = keyvalue.keySet();
//5.再次遍历键的集合
for(String key2 : keyset2){
//6.根据键得到值25 20 21 18
Integer value = keyvalue.get(key2);
System.out.println("\t"+key2+"---"+value);
}
}
--------------------------
HashMap<String, ArrayList<String>> hm = new HashMap<String, ArrayList<String>>();
ArrayList<String> al1 = new ArrayList<String>();
al1.add("曹操");
al1.add("周瑜");
ArrayList<String> al2 = new ArrayList<String>();
al2.add("贾宝玉");
al2.add("林黛玉");
ArrayList<String> al3 = new ArrayList<String>();
al3.add("孙悟空");
al3.add("唐僧");
//1.得到键al1 al2 al3
Set<String> keyset = hm.keySet();
//2.遍历键的集合
for(String key : keyset){
//3.根据键得到值的集合
ArrayList<String> value = hm.get(key);
//4.遍历值的集合
for(String s : value){
System.out.println("\t"+s);
}
}
----------------------------------
ArrayList<HashMap<String, String>> al = new ArrayList<HashMap<String, String>>();
HashMap<String, String> hm1 = new HashMap<String, String>();
hm1.put("周瑜","小乔");
hm1.put("吕布","貂蝉");
al.add(hm1);
HashMap<String, String> hm2 = new HashMap<String, String>();
hm2.put("郭靖","黄蓉");
hm2.put("杨过","小龙女");
al.add(hm2);
HashMap<String, String> hm3 = new HashMap<String, String>();
hm3.put("令狐冲","任盈盈");
hm3.put("林平之","岳灵珊");
al.add(hm3);
//1.遍历键的集合
for(HashMap<String, String> hm : al){
//2.得到键的集合hm1 hm2 hm3
Set<String> keyset = hm.keySet();
//3.遍历键的集合
for(String key : keyset){
//4.根据键得到值
String value = hm.get(key);
System.out.println(key + "---" + value);
}
}
------------------------
HashMap<String, HashMap<String, ArrayList<Student>>> hm = new HashMap<String, HashMap<String, ArrayList<Student>>>();
HashMap<String, ArrayList<Student>> hm1 = 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("曹操", 28);
Student s4 = new Student("典韦", 30);
array2.add(s3);
array2.add(s4);
hm1.put("汉",array1);
hm1.put("魏",array2);
hm.put("三国",hm1);
HashMap<String, ArrayList<Student>> hm2= new HashMap<String, ArrayList<Student>>();
ArrayList<Student> array3 = new ArrayList<Student>();
Student s5 = new Student("唐僧", 27);
Student s6 = new Student("孙悟空", 30);
array1.add(s5);
array1.add(s6);
ArrayList<Student> array4 = new ArrayList<Student>();
Student s7 = new Student("白骨精", 28);
Student s8 = new Student("蜘蛛精", 30);
array2.add(s7);
array2.add(s8);
hm2.put("师徒",array3);
hm2.put("妖怪",array4);
hm.put("西游",hm2);
//得到键的集合三国 西游
Set<String> keyset1 = hm.keySet();
//遍历键的集合
for (String key1 : keyset1) {
System.out.println("key1:"+ key1);
//得到键值对对象
HashMap<String, ArrayList<Student>> keyvalue1 = hm.get(key1);
//得到键的集合魏 蜀 师徒 妖怪
Set<String> keyset2 = keyvalue1.keySet();
//遍历键的集合
for (String key2 : keyset2) {
System.out.println("key2:"+ key2);
//得到对象的集合
ArrayList<Student> keyvalue2 = keyvalue1.get(key2);
//遍历对象的集合
for (Student s : keyvalue2) {
System.out.println(s.getName()+"---"+s.getAge());
}
}
}
集合的嵌套
最新推荐文章于 2022-12-12 09:07:40 发布