Two common methods of hashmap traversal are the use of keyset and entryset for traversal.

Source: Internet
Author: User

First:
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 ();
}
High efficiency. You must use this method in the future!
Second:
Map map = new hashmap ();
Iterator iter = map. keyset (). iterator ();
While (ITER. hasnext ()){
Object key = ITER. Next ();
Object val = map. Get (key );
}
Low efficiency. Try to use less in the future!

Example:
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 (iterator. 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 item.
// Entry. getvalue () returns the value corresponding to this item.
System. Out. Print (entry. getvalue ());
}
System. Out. println ();
System. Out. println (calendar. getinstance (). gettimeinmillis ()-BS );
}
}

The keyset is actually traversed twice. Once converted to iterator, the key pair value 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.

Whether the keyset is faster or entryset is faster. In this example, too many outputs affect the effect. Let's change the example.

Java code
  1. Public ClassHashmaptest {
  2. Private Static IntMaxkey = 1234567;
  3. Public Static VoidMain (string [] ARGs ){
  4. Hashmap =NewHashmap ();
  5. For(IntI = 0; I
  6. Hashmap. Put (I, "thanks ");
  7. }
  8. LongBS = calendar. getinstance (). gettimeinmillis ();
  9. Iterator = hashmap. keyset (). iterator ();
  10. While(Iterator. hasnext ()){
  11. Object key = iterator. Next ();
  12. Object value = hashmap. Get (key );
  13. }
  14. System. Out. Print (calendar. getinstance (). gettimeinmillis ()-BS + ",");
  15. Listhashmap ();
  16. }
  17. Public Static VoidListhashmap (){
  18. Hashmap =NewJava. util. hashmap ();
  19. For(IntI = 0; I
  20. Hashmap. Put (I, "thanks ");
  21. }
  22. LongBS = calendar. getinstance (). gettimeinmillis ();
  23. Iterator it = hashmap. entryset (). iterator ();
  24. Map. Entry entry;
  25. While(It. hasnext ()){
  26. Entry = (Java. util. Map. Entry) it. Next ();
  27. Object key = entry. getkey ();
  28. Object value = entry. getvalue ();
  29. // Object value = hashmap. Get (key );
  30. }
  31. System. Out. Print (calendar. getinstance (). gettimeinmillis ()-BS );
  32. }
  33. }
public class HashMapTest { private static int MAXKEY = 1234567; public static void main(String[] args  {                                                                                                                                                                                         

/*
* First test. When I was 1234567, the output was, 47, 47, and 47 three tests.
* Modify the code. In the while loop of the listhashmap method, object value = hashmap. Get (key)
* Re-test, 63,79 47,78 63,79 three times
*/
Is keyset slower than entryset?
The keyset obtains all keys during the first traversal, And the entryset obtains all keys and values during the first traversal. This step obviously slows down the entryset and looks at the next step.
After iterator, next () of keyset is key, and next () of entryset is map. Entry (including key and value ).
Tests after the code is changed show that the getvalue () of entryset is faster than the get () of map (although they do not know how they are implemented internally, some people say that the get () of Map () small consumption ). Of course, there is no reason to use the changed code. Don't mislead me.

So, I think entryset () seems to be faster, and it takes more time to get the key and value from the first traversal.

However, if you want to sort by key, it seems that keyset is more convenient.

Java code
  1. Object [] key_arr = hashmap. keyset (). toarray ();
  2. Arrays. Sort (key_arr );
  3. For(Object key: key_arr ){
  4. Object value = hashmap. Get (key );
  5. }
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.