Cautious use of keyset: Comparison of 2 traversal modes for HASHMAP

Source: Internet
Author: User

The HashMap store is a key-value pair, so its traversal with the list and set should be different in general.

But Java cleverly handles HashMap's key-value pairs as a monolithic object (Java.util.Map.Entry), which optimizes the traversal of the hashmap, making it no different from the list and set.

The first type:

Java code
    1. Map map = new HashMap ();
    2. Iterator iter = Map.entryset (). Iterator ();
    3. while (Iter.hasnext ()) {
    4. Java.util.Map.Entry Entry = (map.entry) iter.next ();
    5. Object key = Entry.getkey ();
    6. Object val = Entry.getvalue ();
    7. }

The second type:

Java code
    1. Map map = new HashMap ();
    2. Iterator iter = Map.keyset (). Iterator ();
    3. while (Iter.hasnext ()) {
    4. Object key = Iter.next ();
    5. Object val = map.get (key);
    6. }
For example:HASHMAP traversal has two common methods, that is to use keyset and entryset to traverse, but the two traverse speed is different, see the example: Java code
  1. public class Hashmaptest {
  2. public static void Main (string[] args) ... {
  3. HashMap HashMap = new HashMap ();
  4. for (int i = 0; i <; i) ... {
  5. Hashmap.put ("I," that's All ");
  6. }
  7. Long num = Calendar.getinstance (). Gettimeinmillis ();
  8. Iterator Iterator = Hashmap.keyset (). Iterator ();
  9. while (Iterator.hasnext ()) ... {
  10. System.out.print (Hashmap.get (Iterator.next ()));
  11. }
  12. System.out.println ();
  13. System.out.println (Calendar.getinstance (). Gettimeinmillis ()-num);
  14. Listhashmap ();
  15. }
  16. public static void Listhashmap () ... {
  17. Java.util.HashMap HashMap = new Java.util.HashMap ();
  18. for (int i = 0; i <; i) ... {
  19. Hashmap.put ("I," that's All ");
  20. }
  21. Long num = Calendar.getinstance (). Gettimeinmillis ();
  22. Java.util.Iterator it = Hashmap.entryset (). Iterator ();
  23. while (It.hasnext ()) ... {
  24. Java.util.Map.Entry Entry = (java.util.Map.Entry) it.next ();
  25. Entry.getkey () returns the key corresponding to this key
  26. Entry.getvalue () returns the value corresponding to this key
  27. System.out.print (Entry.getvalue ());
  28. }
  29. System.out.println ();
  30. System.out.println (Calendar.getinstance (). Gettimeinmillis ()-num);
  31. }
  32. }
  

Look at the JDK source code, compare two ways of access:

First look at keyset access mode:

Java code
  1. Public set<k> KeySet () {
  2. if (KeySet = = null) {
  3. KeySet = new Abstractset<k> () {
  4. Public iterator<k> Iterator () {
  5. return new iterator<k> () {
  6. Private Iterator<entry<k,v>> i = EntrySet (). Iterator ();
  7. public Boolean hasnext () {
  8. return I.hasnext ();
  9. }
  10. Public K Next () {
  11. Return I.next (). GetKey ();
  12. }
  13. public void Remove () {
  14. I.remove ();
  15. }
  16. };
  17. }
  18. public int size () {
  19. return AbstractMap.this.size ();
  20. }
  21. Public Boolean contains (Object k) {
  22. Return AbstractMap.this.containsKey (k);
  23. }
  24. };
  25. }
  26. return keySet;
  27. }
Conclusion:Through the above test we can see that the use of entryset-way traversal efficiency is better than keyset, so in the development to use EntrySet, try to avoid less use keyset.

Cautious use of keyset: Comparison of 2 traversal modes for HASHMAP

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.