Demand:
Assume that the elements of the HashMap collection are ArrayList. There are 3 of them.
The value of each ArrayList collection is a string.
The elements are as follows, please traverse.
Results:
Three kingdoms
Lvbu
Zhao Yun
Laughing and proud of the lake
Make the Fox rush
The 林平
The Statue of God
Guo Jing
Yang too
1 PackageZl_mapdemo;2 3 Importjava.util.ArrayList;4 ImportJava.util.HashMap;5 ImportJava.util.Set;6 Public classMapandarraylist {7 8 Public Static voidMain (string[] args) {9 Ten //Create HashMap Collection OneHashmap<string, arraylist<string>> HM =NewHashmap<string,arraylist<string>>(); A - //Create a Collection element 1 kingdoms Lub -Arraylist<string> A1 =NewArraylist<string>(); the - //adding elements to collection 1 -A1.add ("Bu"); -A1.add ("Zhao Yun"); + - //add a collection of 1 to the total collection +Hm.put ("Kingdoms", A1); A at //creating a Collection element 2 The Fox rushes 林平 -arraylist<string> A2 =NewArraylist<string>(); - - //add element to collection 2 -A2.add ("Make Fox Punch"); -A2.add ("林平"); in - //Add a collection of 2 to the total collection toHm.put ("The Pride of the lake", a2); + - //creating a Collection element 3 the Condor Guo Jingyang thearraylist<string> A3 =NewArraylist<string>(); * $ //add element to collection 3Panax NotoginsengA3.add ("Guo Jing"); -A3.add ("Yang Too"); the + //Add a collection of 3 to the total collection AHm.put ("The Condor Man", A3); the + - //traversal of the total collection $ //gets the total and the key collection $Set <String> HashSet =Hm.keyset (); - - //Traverse the for(String hashkey:hashset) { - Wuyi //the total and value is arraylist<string> thearraylist<string> list =Hm.get (hashkey); - Wu //traversing the ArrayList collection - for(String s:list) { About //Get Value $ System.out.println (s); - } - - } A } +}
Nesting of Java 18-7 HashMap and ArrayList 1