Four Map Iteration Methods

Source: Internet
Author: User

// Traditional method 1
Map map = new linkedhashmap ();
Map. Put (1, "Zhang San ");
Map. Put (2, "Li Si ");
Map. Put (3, "Wang Wu ");
Set set = map. keyset ();
Iterator = set. iterator ();
While (iterator. hasnext ()){
Int key = (integer) iterator. Next ();
String value = (string) map. Get (key );
System. Out. println (value );
}
// Traditional method 2
Map MAP2 = new linkedhashmap ();
Map2.put ("1", "Zhang San ");
Map2.put ("2", "Li Si ");
Map2.put ("3", "Wang Wu ");
Set set2 = map2.entryset ();
Iterator iterator2 = set. iterator ();
While (iterator2.hasnext ()){
Map. Entry entry = (entry) iterator2.next ();
String key = (string) entry. getkey ();
String value = (string) entry. getvalue ();
System. Out. println (Key + "=" + value );
}
// Enhance the for loop method 1
Map map3 = new linkedhashmap ();
Map3.put ("1", "Zhang San ");
Map3.put ("2", "Li Si ");
Map3.put ("3", "Wang Wu ");
For (Object OBJ: map3.keyset ()){
String key = (string) OBJ;
String value = (string) map3.get (key );
}
// Method 2 of the For Loop Enhancement
Map map4 = new linkedhashmap ();
Map4.put ("1", "Zhang San ");
Map4.put ("2", "Li Si ");
Map4.put ("3", "Wang Wu ");
For (Object OBJ: map4.entryset ()){
Map. Entry entry = (entry) OBJ;
String key = (string) entry. getkey ();
String value = (string) entry. getvalue ();
}

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.