Two kinds of traversal methods of map collection in Java

Source: Internet
Author: User

1. Declare a map:map MAP = new HashMap ();
2. Place values in map, note: Map is stored in key-value form. For example:

Map.put ("sa", "DD");

3. Take value from map: String str = map.get ("sa"). toString (); The result is: str = "DD";

4. Traverse a map to get the key and value from

JDK1.5

Map m = new HashMap ();
For (Object O:map.keyset ()) {
Map.get (o);
}

JDK1.4

Map map = new HashMap ();

Iterator it = Map.entryset (). Iterator ();
while (It.hasnext ())
{
Map.entry Entry = (map.entry) it.next ();
Object key = Entry.getkey ();
Object value = Entry.getvalue ();
}


This article is from "Ghost" blog, please make sure to keep this source http://caizi.blog.51cto.com/5234706/1559473

Two kinds of traversal methods of map collection in Java

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.