集合的巢狀

ZHOU_VIP發表於2017-03-13

HashMap<String,HashMap<String,Integer>> hm = new HashMap<String,HashMap<String,Integer>>();
HashMap<String,Integer> hm1 = new HashMap<String,Integer>();
hm1.put("曹操",25);
hm1.put("周瑜",20);
HashMap<String,Integer> hm2 = new HashMap<String,Integer>();
hm2.put("賈寶玉",21);
hm2.put("林黛玉",18);
hm.put("三國",hm1);
hm.put("紅樓夢",hm2);
//1.得到鍵的集合hm1 hm2
Set<String> keyset1 = hm.keySet();
//2.遍歷鍵的集合
for(String key1 : keyset1){
	//3.得到鍵值對物件
	HashMap<String,Integer> keyvalue = hm.get(key1);
	  //4.得到鍵的集合曹操 周瑜 賈寶玉 林黛玉
	  Set<String> keyset2 = keyvalue.keySet();
	  //5.再次遍歷鍵的集合
	  for(String key2 : keyset2){
			//6.根據鍵得到值25 20 21 18
			Integer value = keyvalue.get(key2);
			System.out.println("\t"+key2+"---"+value);
	  } 
}
--------------------------
HashMap<String, ArrayList<String>> hm = new HashMap<String, ArrayList<String>>();
ArrayList<String> al1 = new ArrayList<String>();
al1.add("曹操");
al1.add("周瑜");
ArrayList<String> al2 = new ArrayList<String>();
al2.add("賈寶玉");
al2.add("林黛玉");
ArrayList<String> al3 = new ArrayList<String>();
al3.add("孫悟空");
al3.add("唐僧");
//1.得到鍵al1 al2 al3
Set<String> keyset = hm.keySet();
//2.遍歷鍵的集合
for(String  key : keyset){
	//3.根據鍵得到值的集合
	ArrayList<String> value = hm.get(key);
	//4.遍歷值的集合
	for(String s : value){
		System.out.println("\t"+s);
	}
}
----------------------------------
ArrayList<HashMap<String, String>> al = new ArrayList<HashMap<String, String>>();
HashMap<String, String> hm1 = new HashMap<String, String>();
hm1.put("周瑜","小喬");
hm1.put("呂布","貂蟬");
al.add(hm1);
HashMap<String, String> hm2 = new HashMap<String, String>();
hm2.put("郭靖","黃蓉");
hm2.put("楊過","小龍女");
al.add(hm2);
HashMap<String, String> hm3 = new HashMap<String, String>();
hm3.put("令狐沖","任盈盈");
hm3.put("林平之","嶽靈珊");
al.add(hm3);
//1.遍歷鍵的集合
for(HashMap<String, String> hm : al){
	//2.得到鍵的集合hm1 hm2 hm3
	Set<String> keyset = hm.keySet();
	//3.遍歷鍵的集合
	for(String key : keyset){
	   //4.根據鍵得到值
		String value = hm.get(key);
		System.out.println(key + "---" + value);
	}
}
------------------------
HashMap<String, HashMap<String, ArrayList<Student>>> hm = new HashMap<String, HashMap<String, ArrayList<Student>>>();
	HashMap<String, ArrayList<Student>> hm1 = new HashMap<String, ArrayList<Student>>();
		ArrayList<Student> array1 = new ArrayList<Student>();
		Student s1 = new Student("劉備", 27);
		Student s2 = new Student("關羽", 30);
		array1.add(s1);
		array1.add(s2);
		ArrayList<Student> array2 = new ArrayList<Student>();
		Student s3 = new Student("曹操", 28);
		Student s4 = new Student("典韋", 30);
		array2.add(s3);
		array2.add(s4);
	hm1.put("漢",array1);
	hm1.put("魏",array2);
hm.put("三國",hm1);

HashMap<String, ArrayList<Student>> hm2= new HashMap<String, ArrayList<Student>>();
		ArrayList<Student> array3 = new ArrayList<Student>();
		Student s5 = new Student("唐僧", 27);
		Student s6 = new Student("孫悟空", 30);
		array1.add(s5);
		array1.add(s6);
		ArrayList<Student> array4 = new ArrayList<Student>();
		Student s7 = new Student("白骨精", 28);
		Student s8 = new Student("蜘蛛精", 30);
		array2.add(s7);
		array2.add(s8);
	hm2.put("師徒",array3);
	hm2.put("妖怪",array4);
hm.put("西遊",hm2);

//得到鍵的集合三國 西遊
Set<String> keyset1 = hm.keySet();
//遍歷鍵的集合
for (String key1 : keyset1) {
	System.out.println("key1:"+ key1);
	//得到鍵值對物件
	HashMap<String, ArrayList<Student>> keyvalue1 = hm.get(key1);
	//得到鍵的集合魏 蜀 師徒 妖怪
	Set<String> keyset2 = keyvalue1.keySet();
	//遍歷鍵的集合
	for (String key2 : keyset2) {
		System.out.println("key2:"+ key2);
		//得到物件的集合
		ArrayList<Student> keyvalue2 = keyvalue1.get(key2);
		//遍歷物件的集合
		for (Student s : keyvalue2) {
			System.out.println(s.getName()+"---"+s.getAge());
		}
	}
}


相關文章