1 package MapTest; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 6 /** 7 * 使用ArrayList集合存放HashMap集合: 8 * 需求创建一个ArrayList集合,存储三个元素,每个元素类型为HashMap集合 9 */ 10 public class HashMapTest04 { 11 public static void main(String[] args) { 12 //创建一个HashMap对象 13 HashMap<String, String> hashMap = new HashMap<>(); 14 //给HashMap添加三个元素 15 HashMap<String, String> hashMap1 = new HashMap<>(); 16 hashMap1.put("MingYuan","20160401041035"); 17 HashMap<String, String> hashMap2 = new HashMap<>(); 18 hashMap2.put("MingYu","20160401041036"); 19 HashMap<String, String> hashMap3 = new HashMap<>(); 20 hashMap3.put("MingWuan","20160401041037"); 21 // System.out.println(hashMap); 22 //把HashMap集合存放入ArrayList集合中 23 ArrayList<HashMap<String, String>> arrayList = new ArrayList<>(); 24 arrayList.add(hashMap1); 25 arrayList.add(hashMap2); 26 arrayList.add(hashMap3); 27 //遍历arraylist集合中的元素(增强for循环) 28 for (HashMap<String, String>Str :arrayList) { 29 System.out.println(Str); 30 } 31 32 } 33 }