Two Methods of hashmap traversal in Java

Source: Internet
Author: User
Conversion] Two Methods of hashmap traversal in Java: http://www.javaweb.cc/language/java/032291.shtmlfirst: Map map = new hashmap (); iterator iter = map. entryset (). iterator (); While (ITER. hasnext () {map. entry entry = (map. entry) ITER. next (); object key = entry. getkey (); object val = entry. getvalue ();} is highly efficient. You must use this method later! Type 2: Map map = new hashmap (); iterator iter = map. keyset (). iterator (); While (ITER. hasnext () {object key = ITER. next (); object val = map. get (key) ;}efficiency is low. Try to use it less in the future! There are two common methods for hashmap traversal: Using keyset and entryset for traversal, but the traversal speed of the two is different. See the example below: public class hashmaptest {public static void main (string [] ARGs )... {hashmap = new hashmap (); For (INT I = 0; I <1000; I )... {hashmap. put ("" I, "thanks");} Long BS = calendar. getinstance (). gettimeinmillis (); iterator = hashmap. keyset (). iterator (); While (iterator. hasnext ())... {system. out. print (hashmap. get (I Terator. next ();} system. out. println (); system. out. println (calendar. getinstance (). gettimeinmillis ()-BS); listhashmap ();} public static void listhashmap ()... {Java. util. hashmap = new Java. util. hashmap (); For (INT I = 0; I <1000; I )... {hashmap. put ("" I, "thanks");} Long BS = calendar. getinstance (). gettimeinmillis (); Java. util. iterator it = hashmap. entryset (). iterator (); While (it. Hasnext ())... {Java. util. map. entry entry = (Java. util. map. entry) it. next (); // entry. getkey () returns the key corresponding to this entry // entry. getvalue () returns the value system corresponding to this item. out. print (entry. getvalue ();} system. out. println (); system. out. println (calendar. getinstance (). gettimeinmillis ()-BS) ;}} the keyset is actually traversed twice, one is converted to iterator, and the value of the key is retrieved from the hashmap at one time. However, entryset only traverses the data for the first time. It puts both the key and value into the entry, so it's faster. Two Methods of hashmap traversal in Java (this tutorial is only for research and learning, does not represent the point of view of Java's Chinese Network/

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.