ArrayList collection nested HashMap set merge traversal.
Demand:
Assume that the elements of the ArrayList collection are hashmap. There are 3 of them.
The keys and values of each HashMap collection are strings.
The elements are as follows, please traverse.
Results:
Zhou Yu---little Joe
Bu---marten cicada
Guo Jing---huang Rong
Yang---little dragon girl.
Make the FOX flush---
林平 's---Keilingshan
1 PackageZl_mapdemo;2 3 Importjava.util.ArrayList;4 ImportJava.util.HashMap;5 ImportJava.util.Set;6 Public classMapandhashmap {7 8 Public Static voidMain (string[] args) {9 Ten //Create a total collection first ArrayList OneArrayList <HashMap> list =NewArrayList <HashMap>(); A - //Create the first collection element -Hashmap<string,string> HM1 =NewHashmap<string,string>(); the - //add elements to this collection -Hm1.put ("Zhou Yu", "Little Joe")); -Hm1.put ("Lv bu", "Marten Cicada"); + - + //Create a second collection element AHashmap<string,string> HM2 =NewHashmap<string,string>(); at - //add elements to this collection -Hm2.put ("Guo Jing", "Huang Rong"); -Hm2.put ("Yang", "Little Dragon Girl")); - - //Create a third collection element inHashmap<string,string> hm3 =NewHashmap<string,string>(); - to //add elements to this collection +Hm3.put ("Make Fox Chong", "Ren Ying"); -Hm3.put ("林平", "Keilingshan"); the * //Add the above 3 small collections to the total collection $ List.add (HM1);Panax Notoginseng List.add (HM2); - List.add (HM3); the + //traversal of the total collection A for(hashmap<string,string>listkey:list) { the //iterate over subsets of HashMap collections + //Get Key Collection -Set <String> HashSet =Listkey.keyset (); $ //traversing the HashSet collection $ for(String hashkey:hashset) { - //Get Value -String result =Listkey.get (hashkey); theSystem.out.println (hashkey+ "\ T" +result); - }Wuyi the } - } Wu -}
Nesting of Java 18-9 HashMap and ArrayList 3