HashMap与ArrayList的相互嵌套

 

HashMap嵌套ArrayList的代码实现

HashMap> hm = new HashMap> ();

HashMap> hm = new HashMap> ();
ArrayList array1 = new ArrayList();
array1.add("吕布");
array1.add("周瑜");
hm.put("三国演义", array1);
ArrayList array2 = new ArrayList();
array2.add("令狐冲");
array2.add("林平之");
hm.put("笑傲江湖", array2);
ArrayList array3 = new ArrayList();
array3.add("郭靖");
array3.add("杨过");
hm.put("神雕侠侣", array3);
Set set = hm.keySet();
for (String key :set) {
System.out.println(key);
ArrayList value = hm.get(key);
for (String s : value) {
System.out.println("\t"+s);

}
}

结果:

三国演义
            吕布
            周瑜
笑傲江湖
           令狐冲 
           林平之
神雕侠侣
          郭靖
          杨过


 

ArrayList嵌套HashMap

ArrayList> list = new ArrayList>();

ArrayList> list = new ArrayList>();
HashMap map1 = new HashMap();
HashMap map2 = new HashMap();
HashMap map3 = new HashMap();
map1.put("周瑜", "小乔");
map1.put("吕布", "貂蝉");
map2.put("郭靖", "黄蓉");
map2.put("杨过", "小龙女");
map3.put("令狐冲", "任盈盈");
map3.put("林平之", "岳灵珊");
list.add(map1);
list.add(map2);
list.add(map3);
for(int i=0;i HashMap map = list.get(i);
Set set = map.keySet();
for (String str : set) {
String value = map.get(str);
System.out.println(str+"-------"+value);
}
}

吕布-------貂蝉
周瑜-------小乔
杨过-------小龙女
郭靖-------黄蓉
令狐冲-------任盈盈
林平之-------岳灵珊

 

转载于:https://www.cnblogs.com/2016-cxp/p/10756930.html

你可能感兴趣的:(HashMap与ArrayList的相互嵌套)