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

Source: Internet
Author: User

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

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.