Analysis of Java HashMap Traversal

Source: Internet
Author: User

Analysis of Java HashMap Traversal

In the original memory, Java HashMap traversal is nothing more than for each or iterator. However, the performance, advantages and disadvantages of traversal are general and unknown. For such basic problems, for Wang Er (Java programming for six years, fortunately I am not programming), I seem to be ashamed to mention it, but it turns out that I still have to "accumulate silicon step ".

① Method 1. iterator iterates keys and searches for values

This method is the most frequently used one. For details, see the following code:

Map
  
   
Map = new HashMap
   
    
(); AddMap (map); long t1 = System. currentTimeMillis (); Iterator
    
     
Keys = map. keySet (). iterator (); while (keys. hasNext () {Integer key = keys. next (); Integer value = map. get (key); keys. remove ();} long t2 = System. currentTimeMillis (); System. out. println ("map. keySet (). iterator () Time consumed: "+ (t2-t1 ));
    
   
  

Previously, in my opinion, this method was quite simple to use. The iterator traversed the keys and obtained the corresponding value from the map through the key. It seems very grounded. However, the disadvantage is that

It is slower and less efficient, and the value obtained through the key is more time-consuming (this method is 20%-200% slower than method #1 in all map implementations ). If you have installed FindBugs, it will detect and warn you that this is an inefficient iteration. This method should be avoided

Seeing this information, I think it is a bit abrupt. It is disappointing that the original favorite map Traversal method is so low. I will make a statistics on the performance time spent later, and pay attention to it later.

② Method 2. Iterator iteration Entry

This method is almost useless before, but method 2 has its key advantages:

You can use iterator to delete map elements. Same method. Excellent performance.
Map
     
      
Map = new HashMap
      
       
(); AddMap (map); long t3 = System. currentTimeMillis (); Iterator
       
        
> Entrys = map. entrySet (). iterator (); while (entrys. hasNext () {Entry
        
         
Entry = entrys. next (); Integer key = entry. getKey (); Integer value = entry. getValue (); entrys. remove ();} long t4 = System. currentTimeMillis (); System. out. println ("map. entrySet (). iterator () Time consumed: "+ (t4-t3 ));
        
       
      
     

Get the map entry object through "map. entrySet (). iterator ()", and then get the key and value through getKey and getValue, which is very straightforward and practical.

③ Method 3: For-Each iteration keys and values

For each is limited to elements in different remove maps, but it is very good to traverse the map.

Map
         
          
Map = new HashMap
          
           
(); AddMap (map); long t5 = System. currentTimeMillis (); for (Integer key: map. keySet () {} for (Integer value: map. values () {} long t6 = System. currentTimeMillis (); System. out. println ("for each map. keySet (), map. values () Time consumed: "+ (t6-t5 ));
          
         

But here, Wang has something to say. According to stackoverflow, this method is better than the fourth method "For-Each iteration entries" (about 10% faster ), but this is not the case in my practice. This method is much slower than the fourth "For-Each iteration entries.

④ Method 4: For-Each iteration entries
Map
           
            
Map = new HashMap
            
             
(); AddMap (map); long t7 = System. currentTimeMillis (); for (Entry
             
              
Entry: map. entrySet () {Integer key = entry. getKey (); Integer value = entry. getValue ();} long t8 = System. currentTimeMillis (); System. out. println ("for each map. entrySet () Time consumed: "+ (t8-t7 ));
             
            
           

This method will not be described much.

⑤ Performance schedule
Order Iterator iterates keys and searches for values Iterator iteration Entry For-Each iteration keys and values For-Each iteration entries
1 Time consumed: 37 Time consumed: 32 Time consumed: 39 Time consumed: 13
2 Time consumed: 29 Time consumed: 18 Time consumed: 32 Time consumed: 15
3 Time consumed: 50 Time consumed: 57 Time consumed: 39 Time consumed: 21
4 Time consumed: 47 Time consumed: 31 Time consumed: 39 Time consumed: 14

It can be summarized as follows:

It is very inefficient to iterate keys and search For values, and the ranking is almost the last or second For-Each iteration entries with the best performance, however, the failure to remove For-Each iteration keys and values does not provide better performance than For-Each iteration entries (about 10% faster ), the data on stackoverflow cannot be completely different from the Iterator iteration Entry solution, which is obviously the most suitable for use and has excellent performance and can be removed.

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.