Java Foundation Enhancements: nested cases for collections

Source: Internet
Author: User

One into the programming deep like the sea, from this sister is passers-by.

How to traverse the list collection:

  

New ArrayList (); // The first way of traversing System.out.println (list); // The second type of traversal, the most common type of foreach New Linkedlist<integer>();          for (integer integer:list) {            System.out.println (integer);        }

How the Map collection is traversed:

Mode 1: Gets the collection of all the key's set traversal keys according to the key, gets each key according to the key to find the value

Private Static void Print (Hashmap<integer, string> h) {        Set<Integer> keySet = h.keyset ();         for(Integer key:keyset) {             System.out.println (key+ "=" +H.get (key));        }    }

Method 2: Find the key and value of the object based on the key value gets all the key values for the collection of object sets traverse the collection of key-value pairs of objects, gets each key-value pair object based on the key value of the object to find the key and value

// h is a map object  for (entry<string, string> entry:h.entryset ()) {             System.out.println (Entry.getkey ()+ "" + Entry.getvalue ());

Case one:

Three kingdoms
Lvbu
Zhou yu
Laughing and proud of the lake
Make the Fox rush
The 林平
The Statue of God
Guo Jing
Yang too

By using the nested implementation of Mao and ArrayList to achieve the above format, the specific implementation code is as follows:

 Packagetest.day14;Importjava.util.ArrayList;ImportJava.util.HashMap;ImportJava.util.LinkedHashMap;ImportJava.util.Map;ImportJava.util.Map.Entry;/*** Map and list nesting *@authorZhangdali **/ Public classHashMapTest2 { Public Static voidMain (string[] args) {//map and list nesting declarationslinkedhashmap<string, arraylist<string>> h =NewLinkedhashmap<>();//creates the first key-value pair of a map collectionarraylist<string> list =NewArraylist<string>(); List.add ("Lui bu"); List.add ("Zhou Yu"); H.put (Kingdoms, list);//creates a second key-value pair for the map collectionArraylist<string> List2 =NewArraylist<string>(); List2.add ("Make the Fox Rush"); List2.add ("林平"); H.put ("The Pride of the lake", list2);//Create a third key-value pair for the map collectionArraylist<string> List3 =NewArraylist<string>(); List3.add ("Guo Jing"); List3.add ("Yang Too"); H.put ("The Statue of God", LIST3);  for(Entry<string, arraylist<string>>Entry:h.entryset ())            {System.out.println (Entry.getkey ()); ArrayList<String> value2 =Entry.getvalue ();  for(String value:value2) {System.out.println ("\ T" +value); }        }    }}

Case TWO:

Zhou Yu-----Little Joe
Sun Quan-----Big Joe
Yang-----little Dragon Girl.
Bu-----Marten cicada

By using nested implementations of list and map, the code is as follows:

 Packagetest.day14;Importjava.util.ArrayList;ImportJava.util.LinkedHashMap;ImportJava.util.Map.Entry; Public classListmaptest { Public Static voidMain (string[] args) {//lis and map nestingarraylist<linkedhashmap<string, string>> list =NewArraylist<linkedhashmap<string, string>>(); Linkedhashmap<string, string> map =NewLinkedhashmap<string, string>(); Map.put ("Zhou Yu", "Little Joe"); Map.put ("Sun Quan", "Big Joe"); Map.put ("Yang Too", "Little Dragon Girl"); Map.put ("Bu", "Marten Cicada");       List.add (map);  for(Linkedhashmap<string, string>map2:list) {             for(Entry<string, string>Entry:map.entrySet ()) {System.out.println (Entry.getkey ()+"-----"+Entry.getvalue ()); intI=0; if(I%2==0 && i!=0) {System.out.println (); I=0; } I++; }       }         }}

Case THREE:

Requirements: There is a sentence set up three layers of protection, each layer must be obtained through a password, please how to design this program '

By using the nested implementations of map and map, the implementation code is as follows:

 Packagetest.day14;ImportJava.util.HashMap;ImportJava.util.Map;ImportJava.util.Scanner; Public classMapqiantaotest { Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); HashMap<integer, string> map =NewHashmap<integer, string>(); Map.put (120, "36 gauge"); HashMap<integer, Hashmap<integer, string>> map2 =NewHashmap<>(); Map2.put (119, map); HashMap<integer, Hashmap<integer, Hashmap<integer, string>>> map3=Newhashmap<Integer, HashMap<integer, Hashmap<integer, string>>>(); Map3.put (110, MAP2);  while(true) {System.out.println ("Please enter first level password"); intPWD =Sc.nextint (); HashMap<integer, Hashmap<integer, string>> MAP4 =Map3.get (PWD); if(NULL!=MAP4) {                 while(true) {System.out.println ("Please enter a second level password"); intPWD2 =Sc.nextint (); HashMap<integer, string> map5 =Map4.get (PWD2); if(map5!=NULL) {                          while(true) {System.out.println ("Please enter a third level password"); intPWD3 =Sc.nextint (); String result=Map5.get (PWD3); if(result!=NULL) {System.out.println (result);  Break; } System.out.println ("Last level password input error, re-enter"); }                         Break; } System.out.println ("Second level password input error, re-enter"); }                 Break; } System.out.println ("First level password input error, re-enter"); }            }}

Conclusion: The temptation to live, can withstand loneliness.

Java Foundation Enhancements: nested cases for collections

Related Article

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.