Several common methods of map collection traversal

Source: Internet
Author: User
Tags set set

  1. Import Java.util.HashMap;
  2. Import Java.util.Iterator;
  3. Import Java.util.Map;
  4. Import Java.util.Set;
  5. Import Java.util.Map.Entry;
  6. /**
  7. * The map set is as unordered as the set set
  8. * Several common methods for map collection traversal
  9. * @author Owner
  10. *
  11. */
  12. Public class MapTest5 {
  13. public static void Main (string[] args) {
  14. map<string, string> map = new hashmap<string, string> ();
  15. Map.put ("A", "Zhangsan");
  16. Map.put ("B", "Lisi");
  17. Map.put ("C", "Wangwu");
  18. SYSTEM.OUT.PRINTLN (map);
  19. /** 
  20. * Method One traversal map
  21. */
  22. set<string> KeySet = Map.keyset ();
  23. For (iterator<string> Iterator = Keyset.iterator (); Iterator.hasnext ();) {
  24. String key = Iterator.next ();
  25. String value = Map.get (key);
  26. System.out.println (key+"=" +value);
  27. }
  28. System.out.println ("**********************");
  29. /** 
  30. * Method Two Calendar map
  31. */
  32. For (String Key:map.keySet ()) {
  33. System.out.println (key+"=" +map.get (key));
  34. }
  35. System.out.println ("**********************");
  36. /** 
  37. * Method Three traversal map, it is recommended to use this method to traverse the map collection, especially when the capacity is large
  38. */
  39. for (map.entry<string, string> entry:map.entrySet ()) {
  40. System.out.println (Entry.getkey () +"=" +entry.getvalue ());
  41. }
  42. System.out.println ("**********************");
  43. /** 
  44. * Method Four Calendar Map
  45. */
  46. set<entry<string, string>> entryset = Map.entryset ();
  47. for (iterator<map.entry<string, string>> Iterator = Entryset.iterator (); Iterator.hasnext ();) {
  48. map.entry<string, string> Entry = Iterator.next ();
  49. System.out.println (Entry.getkey () +"=" +entry.getvalue ());
  50. }
  51. System.out.println ("**********************");
  52. /** 
  53. * Method Five, this method iterates over all the values
  54. */
  55. For (String value:map.values ()) {
  56. System.out.println (value);
  57. }
  58. }
  59. }

Several common methods of map collection traversal

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.