Three Map traversal methods and three map Methods

Source: Internet
Author: User

Three Map traversal methods and three map Methods

Package decorator; import java. util. collection; import java. util. hashMap; import java. util. map; import java. util. map. entry; import java. util. set; import org. junit. before; import org. junit. test;/*** three Map traversal methods 1. keySet () 2. values () 3. after the entrySet () * method is used to obtain the Set, you can use foreach or iterator instead of, because the data structure determines ** @ author Administrator **/public class MapCycle {Map <Integer, String> map; // prepare the data @ Before public void testData () {map = new HashMap <> (); map. put (1, "lingyi"); map. put (2, "Ling 2"); map. put (3, "Ling 3"); map. put (4, "Ling 4"); map. put (5, "Ling 5");}/** test three methods. These three methods are used to traverse the Set, therefore, you can use foreach or Iterator ** // Method 1: The keySet () method to obtain the Set (key) @ Test public void testFirst () {Set <Integer> set = map. keySet (); for (Integer integer: set) {System. out. println (map. get (integer) ;}// Method 2: Obtain the Collection (value) @ Test public void testSecond () {Collection <String> collection = map. values (); for (String string: collection) {System. out. println (string) ;}// method 3: The entrySet () method obtains the Set <Entry <key, value >>@test public void testThird () {Set <Entry <Integer, String> entries = map. entrySet (); for (Entry <Integer, String> entry: entries) {System. out. println (entry. getValue ());}}}

  

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.