Nesting of Java 18-7 HashMap and ArrayList 1

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.