Map嵌套和ArrayList嵌套Map(转)
集合嵌套之HashMap嵌套HashMap
基础班
张三 20
李四 22
就业班
王五 21
赵六 23
public class Test5 {
public static void main(String[] args) {
HashMap map = new HashMap<>();
map.put("张三",20);
map.put("李四",22);
HashMap map1 = new HashMap<>();
map1.put("王五",21);
map1.put("赵六",23);
HashMap> mapmax = new HashMap<>();
mapmax.put("基础班",map);
mapmax.put("就业班",map1);
Set>> entries = mapmax.entrySet();
for (Map.Entry> entry : entries) {
System.out.println(entry.getKey());
Set> entries1 = entry.getValue().entrySet();
for (Map.Entry stringIntegerEntry : entries1) {
System.out.println("\t"+stringIntegerEntry.getKey()+" "+stringIntegerEntry.getValue());
}
}
}
}
Arraylist嵌套HashMap
public class Test3 {
public static void main(String[] args) {
HashMap map = new HashMap<>();
map.put("周瑜","小乔");
map.put("吕布","貂蝉");
HashMap map1 = new HashMap<>();
map1.put("郭靖","黄蓉");
map1.put("杨过","小龙女");
HashMap map2 = new HashMap<>();
map2.put("令狐冲","任盈盈");
map2.put("林平之","岳灵珊");
ArrayList> list = new ArrayList<>();
list.add(map);
list.add(map1);
list.add(map2);
for (HashMap s : list) {
Set strings = s.keySet();
for (String s1 : strings) {
String s2 = s.get(s1);
System.out.println("\t"+s1+"---"+s2);
}
System.out.println();
}
}
}
————————————————
版权声明:本文为CSDN博主「taraex」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/taraex/article/details/90243965