[Java learning notes] examples of keySet, entrySet, and values in Map sets, keysetentryset

Source: Internet
Author: User

[Java learning notes] examples of keySet, entrySet, and values in Map sets, keysetentryset

1 import java. util. collection; 2 import java. util. hashMap; 3 import java. util. iterator; 4 import java. util. map; 5 import java. util. set; 6 7 public class MapDemo {8 9 public static void main (String [] args) {10 Map <Integer, String> map = new HashMap <Integer, String> (); 11 12 13 map. put (8, "zhaoliu"); 14 map. put (9, "zhaoliu"); 15 map. put (1, "xiaoqiang"); 16 map. put (6, "wangcai"); 17 map. put (7, "zhaoliu"); 18 Map. put (99, "zhaoliu"); 19 map. put (87, "xiaoqiang"); 20 map. put (42, "wangcai"); 21 22 show1 (map); 23 24 show2 (map); 25 26 showValue (map ); 27 28} 29 30 public static void showValue (Map <Integer, String> map) {31 Collection <String> values = map. values (); 32 Iterator <String> it = values. iterator (); 33 while (it. hasNext () 34 {35 System. out. println (it. next (); 36} 37} 38 39 public static void show2 (Map <Integer, String> map) {40 // You Can iterate 41 by converting Map to Set. // another method is found. EntrySet42 // This method stores the key-value ing relationship as an object in the Set, and the ing type is Map. entry type (Marriage Certificate) 43 44 Set <Map. entry <Integer, String> entrySet = map. entrySet (); 45 Iterator <Map. entry <Integer, String> it = entrySet. iterator (); 46/* 47 * can also be written as Iterator <Map. entry <Integer, String> it = map. entrySet (). iterator (); 48 */49 50 while (it. hasNext () 51 {52 Map. entry <Integer, String> me = it. next (); 53 Integer key = me. getKey (); 54 String va Lue = me. getValue (); 55 System. out. println (key + ":" + value); 56 57} 58} 59 60 public static void show1 (Map <Integer, String> map) {61 // retrieve all elements in the map. 62 // Principle: The keySet method is used to obtain the Set where all the keys in the map are located, and each key is obtained through the Set iterator, 63 // obtain the value of each key through the get method of the map set. 64 65 Iterator <Integer> it = map. keySet (). iterator (); 66/* 67 * is equivalent to Set <Integer> keySet = map. keySet (); 68 * Iterator <Integer> it = keySet. iterator (); 69*70 */71 72 while (it. hasNext () 73 {74 Integer key = it. next (); 75 String value = map. get (key); 76 System. out. println (key + "=" + value); 77} 78} 79 80}

 

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.