码迷,mamicode.com
首页 > 编程语言 > 详细

java 18 - 8 HashMap和ArrayList的嵌套2

时间:2016-09-26 19:37:49      阅读:236      评论:0      收藏:0      [点我收藏+]

标签:

 

数据乱扯的
结果:
  小说
    三大名著
           三国演义
              吕布    30
              赵云    28
           西游记
              孙悟空   600
              唐僧     30
    武侠小说
           笑傲江湖
              令狐冲    26
              林平之    28
          神雕侠侣
              郭靖      40
              杨过    23


  分析:
    总的集合是      HashMap<String,HashMaP<String,ArrayList<Person>>>,
      子集合:HashMaP<String,ArrayList<Person>>   ArrayList<Person>

 

 

 1 package zl_MapDemo;
 2 
 3 import java.util.ArrayList;
 4 import java.util.HashMap;
 5 import java.util.Set;
 6 public class HashMapDemo {
 7 
 8     public static void main(String[] args) {
 9         
10         //创建总集合小说
11         HashMap<String,HashMap<String,ArrayList<Person>>> hm1 = 
12                 new  HashMap<String,HashMap<String,ArrayList<Person>>>();
13         
14         //创建三大名著子集合
15         HashMap<String,ArrayList<Person>> hm2 = new HashMap<String,ArrayList<Person>>();
16         //创建三国演义集合
17         ArrayList<Person> list1 = new ArrayList<Person>();
18         //创建西游记集合
19         ArrayList<Person> list2 = new ArrayList<Person>();
20         
21         //创建三国演义和西游记集合的元素
22         Person p1 = new Person ("吕布",30);
23         Person p2 = new Person ("赵云",28);
24         Person p3 = new Person ("孙悟空",600);
25         Person p4 = new Person ("唐僧",26);
26         
27         //把元素添加到三国演义和西游记的集合中
28         list1.add(p1);
29         list1.add(p2);
30         list2.add(p3);
31         list2.add(p4);
32         
33         //把三国演义和西游记集合添加到三大名著集合中
34         hm2.put("三国演义", list1);
35         hm2.put("西游记", list2);
36         
37         //再把三大名著集合添加到小说集合中
38         hm1.put("三大名著", hm2);
39         
40         //创建武侠小说集合
41         HashMap<String,ArrayList<Person>> wxhm = new HashMap<String,ArrayList<Person>>();
42         
43         //创建笑傲江湖和神雕侠侣集合
44         ArrayList<Person> array1 = new ArrayList<Person>();
45         ArrayList<Person> array2 = new ArrayList<Person>();
46 
47         //创建笑傲江湖和神雕侠侣集合的元素
48         Person s1 = new Person("令狐冲",26);
49         Person s2 = new Person("林平之",28);
50         Person s3 = new Person("郭靖",40);
51         Person s4 = new Person("杨过  ",23);
52         
53         //把这些元素分别添加到两个集合中
54         array1.add(s1);
55         array1.add(s2);
56         array2.add(s3);
57         array2.add(s4);
58         
59         //把两个集合添加到武侠小说集合中
60         wxhm.put("笑傲江湖", array1);
61         wxhm.put("神雕侠侣", array2);
62         
63         //把武侠小说集合添加到小说集合中
64         hm1.put("武侠小说", wxhm);
65         
66         //遍历
67         
68         //获取小说集合的键集合
69         Set<String> hm1Set = hm1.keySet();
70         //遍历
71         for(String hm1key : hm1Set){
72             //小说集合的值是HashMap<String,ArrayList<Person>>
73             HashMap<String,ArrayList<Person>> xsvalue= hm1.get(hm1key);
74             //创建三大名著和武侠小说这个集合的键集合
75             Set<String> xsSet = xsvalue.keySet();
76             //遍历
77             for(String xskey : xsSet){
78                 System.out.println(xskey);
79             //这个集合的值是ArrayList<Person>
80             ArrayList<Person> value = xsvalue.get(xskey);
81             
82             //遍历ArrayList集合
83             for(Person result : value){
84                 System.out.println("\t "+result.getName()+"\t"+result.getAge());
85             }
86         }
87         }
88     }
89 
90 }

 

java 18 - 8 HashMap和ArrayList的嵌套2

标签:

原文地址:http://www.cnblogs.com/LZL-student/p/5910070.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!