1. Traversal of the Map collection (key-value pairs object-find key and value)
Map--Couples on
Ideas:
A: Get a collection of all marriage certificates
B: Walk through a collection of marriage certificates, get every marriage certificate
C: Obtain husband and wife according to marriage certificate
Transformation:
A: Gets the collection of all key-value pairs of objects
B: Iterate over the collection of key-value pairs of objects and get each key-value pair of objects
C: Gets the key and value for the object based on the key value
2. code example:
1 Packagecn.itcast_01;2 3 ImportJava.util.HashMap;4 ImportJava.util.Map;5 ImportJava.util.Set;6 7 /*8 * Look at one of the ways we started:9 * set<map.entry<k,v>> entryset (): Returns a collection of key-value pairs of objects Ten */ One Public classMapDemo4 { A Public Static voidMain (string[] args) { - //To create a collection object -map<string, string> map =NewHashmap<string, string>(); the - //Create an element and add it to the collection -Map.put ("Yang", "Little Dragon Girl")); -Map.put ("Guo Jing", "Huang Rong"); +Map.put ("Yang Kang", "Munians"); -Map.put ("Chen Xuanfeng", "MUI Wind"); + A //gets the collection of all key-value pairs of objects atSet<map.entry<string, string>> set =Map.entryset (); - //iterates over the collection of key-value pairs of objects, getting each key-value pair of objects - for(Map.entry<string, string>Me:set) { - //get keys and values on objects based on key values -String key =Me.getkey (); -String value =Me.getvalue (); inSYSTEM.OUT.PRINTLN (key + "---" +value); - } to } +}
Run the results as follows:
Java Basic Knowledge Hardening Collection Framework note 53:map collection of the traversal key value of the map collection of the object to find the key and value