Java traversal map in several ways

Source: Internet
Author: User

1. Several ways to traverse the map:
Private hashtable<string, string> emails = new hashtable<string, string> ();

Method one: With EntrySet ()
Iterator it = Emails.entryset (). Iterator ();
while (It.hasnext ()) {
Map.entry m= (Map.entry) it.next ();
Logger.info ("email-" + m.getkey () + ":" + m.getvalue ());
}

Method Two: Direct re-circulation
For (map.entry<string, string> m:emails.entryset ()) {
Logger.info ("email-" + m.getkey () + ":" + m.getvalue ());
}

Method Three: With Keyset ()
Iterator it = Emails.keyset (). Iterator ();
while (It.hasnext ()) {
String key;
key= (String) it.next ();
Logger.info ("email-" + key + ":" + emails.get (key));
}

Method Four: First convert the HashMap to set collection, then iterate the output, but the object is obtained
Map AA = new HashMap ();
Aa.put ("Tmp1", New Object ()); Append substitution with the same function.
Aa.remove ("Temp1"); Delete
for (Iterator i = aa.values (). Iterator (); I.hasnext ();) {
Object temp = I.next ();
}//Traversal

Several ways Java traverses a map

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.