Java HashMap Traversal Practice

Source: Internet
Author: User

In the original memory, the Java hashmap traversal, is nothing more than for each or iterator, but as to the performance of the Times, the pros and cons, general and not know. For this fundamental issue, for Wang ER (Java programming 6, fortunately my direction is not programming) I seem to be ashamed to mention, but it turns out that I have to "silicon step."

① method One, iterator iterate keys and search values

This method is most frequently used by me, not one of which is described in the following code:

new HashMap<Integer, Integer>();addMap(map);long t1 = System.currentTimeMillis();Iterator<Integer> 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()耗时:" + (t2 - t1));

In my view, the method used to be fairly concise, traversing the keys through iterator, and then retrieving the corresponding value from the map via key, which seems to be very grounded. But the downside is that

It's slower and less efficient, and it's time consuming to get value values by key (this method is 20%-200% slower than method # # in all maps that implement the map interface). If you install FindBugs, it will detect and warn you that this is an inefficient iteration. This method should avoid

See this message, I feel a bit abrupt, how the original favorite of a map traversal way so low, it is disappointing. I'll do a statistic about the performance time I spend, and I'll pay attention later.

② method Two, iterator iterative entry

This approach I used to hardly use, but method two has its key advantages:

    1. The elements of the map can be deleted by iterator. Method one the same.
    2. Excellent performance.
new HashMap<Integer, Integer>();addMap(map);long t3 = System.currentTimeMillis();Iterator<Entry<Integer, Integer>> entrys = map.entrySet().iterator();while (entrys.hasNext()) {    Entry<Integer, Integer> entry = entrys.next();    Integer key = entry.getKey();    Integer value = entry.getValue();    entrys.remove();}long t4 = System.currentTimeMillis();System.out.println(" map.entrySet().iterator()耗时:" + (t4 - t3));

Obtaining the entry object of the map through the "Map.entryset (). Iterator ()" and then using the Getkey,getvalue for key and value is very straightforward and practical.

③ method Three: For-each iteration keys and values

For each one limitation is different from the elements in the remove map, but traversing the map is still very good.

new HashMap<Integer, Integer>();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()耗时:" + (t6 - t5));

But here, Wang Er has something to say, according to StackOverflow, the method is better than the next fourth method of "For-each iterative Entries" (about 10% faster), but in my practice it's not the same way as the fourth " For-each iteration Entries "is much slower.

④ method Four: For-each iterative entries
new HashMap<Integer, Integer>();addMap(map);long t7 = System.currentTimeMillis();for (Entry<Integer, Integer> entry : map.entrySet()) {    Integer key = entry.getKey();    Integer value = entry.getValue();}long t8 = System.currentTimeMillis();System.out.println("for each map.entrySet()耗时:" + (t8 - t7));

This approach does not introduce much.

⑤ Performance Schedule
Order iterator iterate keys and search values Iterator Iteration Entry For-each iterate keys and values For-each Iteration Entries
1 Time: 37 Time: 32 Time: 39 Time: 13
2 Time: 29 Time: 18 Time: 32 Time: 15
3 Time: 50 Time: 57 Time: 39 Time: 21
4 Time: 47 Time: 31 Time: 39 Time: 14

Can be summarized as follows:

    1. Iterate keys and search values very inefficient, ranked almost in the penultimate or second
    2. For-each Iterative entries performance is best, but cannot remove
    3. For-each iterative keys and values are no more than For-each iterative entries performance (about Fast 10%), and the data on StackOverflow cannot be fully disagreed with
    4. The Iterator iterative entry scheme is clearly best suited for use, with excellent performance and the ability to remove
Reference articles
    • HashMap traversal

Thank you for reading "Silent Wang er Blog", if Wang ER's blog to bring you a hint of help or touched, I (that is, Wang II) will not be very honored.
If you happen to like it, you can leave a message or send it to me, which will be the strongest motivation for me to tinker with more excellent articles.

Java HashMap Traversal Practice

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.