标签:strong print new 取出 hashmap 一个 lis lang add
public class Test{
public static void main(String[] args){
/*
假如有两个班的学生姓名,它们分别存储在两个集合中:
*/
//第一个班
ArrayList<String> list1 = new ArrayList<>();
list1.add("迪丽热巴");
list1.add("古力娜扎");
list1.add("柳岩");
list1.add("杨幂");
//第二个班
ArrayList<String> list2 = new ArrayList<>();
list2.add("蔡徐坤");
list2.add("杨坤");
list2.add("陈伟霆");
list2.add("李易峰");
//将两个集合存储到一个集合中
ArrayList<ArrayList<String>> allList = new ArrayList<>();
allList.add(list1);
allList.add(list2);
//遍历allList,取出每个ArrayList
for(ArrayList<String> list : allList){
//遍历每个班的ArrayList
for(String s : list){
System.out.println(s);
}
}
}
}
public class Test{
public static void main(String[] args){
/*
有两个班的学员,分别存储在两个Map中
*/
//第一个班:
Map<String,String> map1 = new HashMap<>();
map1.put("it001","迪丽热巴");
map1.put("it002","古力娜扎");
//第二个班:
Map<String,String> map2 = new HashMap<>();
map6.put("heima001","蔡徐坤");
map2.put("heima002","李易峰");
//将两个班的map存储到一个ArrayList中
ArrayList<Map<String,String>> allList = new ArrayList<>();
allList.add(map1);
allList.add(map2);
//遍历allList,取出每个Map
for(Map<String,String> map : allList){
//遍历map
Set<String> keys = map.keySet();
for(String key : keys){
System.out.println(key + " - " + map.get(key));
}
}
}
}
public class Test{
public static void main(String[] args){
/*
有两个班,班号分别为:"黑马188期"和"黑马189期",两个班学员的姓名分别存储在两个Map中
*/
//"黑马188期":
Map<String,String> map1 = new HashMap<>();
map1.put("it001","迪丽热巴");
map1.put("it002","古力娜扎");
//"黑马189期":
Map<String,String> map2 = new HashMap<>();
map2.put("heima001","蔡徐坤");
map2.put("heima002","李易峰");
//将两个班的Map连同对应的"班号"一同存储在一个Map中
Map<String,Map<String,String>> allMap = new HashMap<>();
allMap.put("黑马188期",map1);
allMap.put("黑马189期",map2);
//遍历allMap
Set<String> keys = allMap.keySet();
for(String k : keys){
System.out.println(k + ":");
//取出对应的map
Map<String,String> map = allMap.get(k);
//遍历map
Set<String> keys2 = map.keySet();
for(String k2 : keys2){
System.out.println(k2 + " = " + map.get(k2));
}
}
}
}
标签:strong print new 取出 hashmap 一个 lis lang add
原文地址:https://www.cnblogs.com/6ovo6/p/14951666.html