Java Traversal Map Object

Source: Internet
Author: User

Map as our common container, in the study and work with a lot of, the following describes several ways to traverse the Map collection object for your reference

The first, in the For-each loop, uses the entry object to traverse each key-value pair in the map, a method that is more commonly used, However, For-each is introduced after java1.5, so it requires a version above 1.5 to be used, and for a foe-each traversal of an empty map throws a null pointer exception, you need to be aware that the map is empty

The second, similar to the first one, is also a for-each loop, but the object of the loop is a key or a value, and the notation is similar

The third type, using an iterator to traverse, puts an example

Of course, in addition to the entry object that can iterate map, but also can iterate the key or value, here is not introduced, this method has the iterator traversal container of a bit, the operation is rich, the use of the history is relatively long, performance is also very advantageous

This is a few commonly used and good performance of the traversal method, the next time you can compare their performance.

Java Traversal 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.