Traversal of the Java map object

Source: Internet
Author: User

In general, the most common implementation class for a map is HashMap.

The traversal of a map is also an iterative

1. Use entries to traverse in the For-each loop (both key and value are taken)

New Hashmap<string, string>();      for (map.entry<string, string>map.entryset ()) {        System.out.println(entry.getkey () + ", Value:" +  entry.getvalue ());    }

2. Traverse keys or values (value or Fetch key) in the For-each loop

New Hashmap<string, string>();     // Fetch key    for  Map.keyset ()) {        System.out.println ("key:" + key);   }    //Fetch value    for  map.values ()) {        System.out.println ("value:" + value);  }  

3. Using iterator traversal

New Hashmap<string, string>();    Iterator<map.entry<string, string>> entries =  map.entryset (). Iterator ();      while (Entries.hasnext ()) {        map.entry<string, string> Entry = entries.next ();      System.out.println ("Key:" + entry.getkey () + ", Value:" + Entry.getvalue ());    }

4. Key to find the value traversal get (key), it is generally known that a key directly get the value, no longer loop traversal.

Traversal of the Java map object

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.