4 ways to traverse a map object in Java

Source: Internet
Author: User

11111401

In Java, map based on the underlying data structure, there are many different implementations, such as hash HashMap, linked list linkedmap, hash list linkedhashmap, tree table (binary tree) TreeMap and so on.

This article discusses several different ways of traversing the most commonly used hashmap hash lists in our programming, as well as the comparison between the methods of writing and efficiency.

First, prepare a map hash table and store 100,000 records.

Key is starting from Key0 to key99999

Value starts from Hello0 to hello99999

[Java]View PlainCopy
    1. Public static void Main (string[] args) {
    2. map<string,object> map=New hashmap<string,object> ();
    3. For (int i=0;i<100000;i++) {
    4. Map.put ("key" +i, "Hello" +i);
    5. }
    6. }



Traversal mode one: EntrySet () + Enhanced for loop

[Java]View PlainCopy
  1. MAP Traversal mode one//2425.7
  2. static void Iterator_type1 (map<string,object> Map) {
  3. Set<map.entry<string, object>> set= map.entryset ();
  4. long start=new Date (). GetTime ();
  5. for (map.entry<string, object> item:set) {
  6. String Key=item.getkey ();
  7. Object Value=item.getvalue ();
  8. System.out.println ("key:" +key+ "" +"value:" +value);
  9. }
  10. long end=new Date (). GetTime ();
  11. SYSTEM.OUT.PRINTLN ("Traversal time is:" + (End-start) +"millisecond");
  12. }


Traverse mode two: EntrySet () +iterator Iteration

[Java]View PlainCopy
  1. MAP Traversal Mode II 2408.9
  2. static void Iterator_type2 (map<string,object> Map) {
  3. Set<map.entry<string, object>> set= map.entryset ();
  4. Iterator<map.entry<string, object>> it= set.iterator ();
  5. long start=new Date (). GetTime ();
  6. While (It.hasnext ()) {
  7. Map.entry<string, object> item= it.next ();
  8. String Key=item.getkey ();
  9. Object Value=item.getvalue ();
  10. System.out.println ("key:" +key+ "" +"value:" +value);
  11. }
  12. long end=new Date (). GetTime ();
  13. SYSTEM.OUT.PRINTLN ("Traversal time is:" + (End-start) +"millisecond");
  14. }


Traverse mode three: KeySet () + iterator iteration

[Java]View PlainCopy
  1. MAP Traversal mode three 2441.0
  2. static void Iterator_type3 (map<string,object> Map) {
  3. Set<string> keys= Map.keyset ();
  4. Iterator<string> it= keys.iterator ();
  5. long start=new Date (). GetTime ();
  6. While (It.hasnext ()) {
  7. String Key=it.next ();
  8. Object Value=map.get (key);
  9. System.out.println ("key:" +key+ "" +"value:" +value);
  10. }
  11. long end=new Date (). GetTime ();
  12. SYSTEM.OUT.PRINTLN ("Traversal time is:" + (End-start) +"millisecond");
  13. }


Traverse mode four: KeySet () + Enhanced for loop

[Java]View PlainCopy
  1. MAP Traversal mode four 2445.5
  2. static void Iterator_type4 (map<string,object> Map) {
  3. Set<string> keys= Map.keyset ();
  4. long start=new Date (). GetTime ();
  5. For (String key:keys) {
  6. Object Value=map.get (key);
  7. System.out.println ("key:" +key+ "" +"value:" +value);
  8. }
  9. long end=new Date (). GetTime ();
  10. SYSTEM.OUT.PRINTLN ("Traversal time is:" + (End-start) +"millisecond");
  11. }

4 ways to traverse a map object 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.