Traverse the entire hashmap cyclically, hashtable

Source: Internet
Author: User

It is found that hashmap uses the following format to traverse the entire hashmap hashtable in a loop.

Each element in the returned set is of the map. Entry type.

Java code private hashtable <string, string> emails = new hashtable <string, string> ();
  1. // Method 1: entryset ()
  2. // Iterator it = emails. entryset (). iterator ();
  3. // While (it. hasnext ()){
  4. // Map. Entry M = (Map. Entry) it. Next ();
  5. // Logger.info ("email-" + M. getkey () + ":" + M. getvalue ());
  6. //}
  7. // Method 2: directly recycling
  8. For (Map. Entry <string, string> M: emails. entryset ()){
  9. Logger.info ("email-" + M. getkey () + ":" + M. getvalue ());
  10. }
  11. // Method 3: Use keyset ()
  12. Iterator it = emails. keyset (). iterator ();
  13. While (it. hasnext ()){
  14. String key;
  15. Key = (string) it. Next ();
  16. Logger.info ("email-" + key + ":" + emails. Get (key ));
  17. }
Private hashtable <string, string> emails = new hashtable <string, string> (); // Method 1: entryset () // iterator it = emails. entryset (). iterator (); // while (it. hasnext () {// map. entry M = (map. entry) it. next (); // logger.info ("email-" + M. getkey () + ":" + M. getvalue (); // Method 2: directly recycle for (map. entry <string, string> M: emails. entryset () {logger.info ("email-" + M. getkey () + ":" + M. getvalue ();} // method 3: Use keyset () iterator it = emails. keyset (). iterator (); While (it. hasnext () {string key; Key = (string) it. next (); logger.info ("email-" + key + ":" + emails. get (key ));}

In addition, we can convert hashmap into a collection and then iterate the output, but the obtained object

Java code
  1. Map AA = new hashmap ();
  2. AA. Put ("tmp1", new object (); // append with the same function.
  3. AA. Remove ("temp1"); // Delete
  4. For (iterator I = AA. Values (). iterator (); I. hasnext ();){
  5. Object temp = I. Next ();
  6. } // Traverse
Map AA = new hashmap (); AA. put ("tmp1", new object (); // replace append with the same function. AA. remove ("temp1"); // Delete For (iterator I = AA. values (). iterator (); I. hasnext ();) {object temp = I. next ();} // traverse

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.