Four ways Java traverses a map object

Source: Internet
Author: User

Way one This is the most common and in most cases also the most desirable way to traverse. Used when the key value is required.

1234 Map<Integer, Integer> map = newHashMap<Integer, Integer>(); for (Map.Entry<Integer, Integer> entry : map.entrySet()) {   System.out.println("Key = " + entry.getKey() + ", Value = "+ entry.getValue()); }

Method Two traverses the keys or values in the For-each loop.

If you only need the keys or values in the map, you can implement the traversal through keyset or values instead of using EntrySet.

123456789 map<integer, integer> Map = /code>new  hashmap<integer, integer> (); //traverse the keys in the map for   (Integer Key:map.keySet ()) { &NBSP;&NBSP; system.out.println ( "Key ="  + Key); //traverse the values in map for   (Integer value:map.values ()) {     system.out.println ( "Value ="  + value); }

This method is slightly better than entryset traversal (10% faster), and the code is cleaner.

Method three uses iterator traversal

using generics :

123456 Map<Integer, Integer> map = newHashMap<Integer, Integer>(); Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator(); while (entries.hasNext()) {   Map.Entry<Integer, Integer> entry = entries.next();   System.out.println("Key = " + entry.getKey() + ", Value = "+ entry.getValue()); }

Do not use generics:

12345678 map Map = new  hashmap (); iterator entries = Map.entryset (). Iterator (); while   (Entries.hasnext ()) { &NBSP;&NBSP; map.entry Entry = (map.entry) entries.next (); &NBSP;&NBSP; integer key = (Integer) entry.getkey (); &NBSP;&NBSP; integer value = (Integer) entry.getvalue (); &NBSP;&NBSP; system.out.println ( + key + + Value); }

You can also apply the same method to keyset and values.

This way of looking redundant has its advantages. First, this is the only way to traverse the map in the old version of Java. Another benefit is that you can delete the entries by calling Iterator.remove () over the duration, and the other two methods cannot. According to Javadoc, if you try to use this method in a For-each traversal, the result is unpredictable.

In terms of performance, this method is similar to the performance of for-each traversal (i.e., method two).

Method Four, through the key to find the value traversal (low efficiency)

1234 Map<Integer, Integer> map = newHashMap<Integer, Integer>(); for (Integer key : map.keySet()) {   Integer value = map.get(key);   System.out.println("Key = " + key + ", Value = "+ value);

As an alternative to method one, the code looks cleaner, but in fact it is quite slow and inefficient. Because the value from the key is a time-consuming operation (compared to method one, the method is slower 20%~200% in different map implementations). If you install FindBugs, it will check and warn you about what is inefficient traversal. So try to avoid it.

Summarize

Use method Two if you only need keys or values. You must use method three if you are using a language version that is lower than Java 5, or if you plan to delete entries over time. Otherwise, use method one (the key value is all).

Four ways Java traverses a map object

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.