Introduction to map. Entry class

Source: Internet
Author: User
Tags map class
Are you tired of getting keywords from map every time and getting the corresponding values? Using the map. Entry class, you can get all the information at the same time. The standard MAP access method is as follows:

Set keys = map. keyset ();
If (Keys! = NULL ){
Iterator = keys. iterator ();
While (iterator. hasnext ()){
Object key = iterator. Next ();
Object value = map. Get (key );
;....
;}
}

Then there is a problem with this method. After obtaining keywords from map, we must repeat the returned values to map to obtain the relative values, which is cumbersome and time-consuming.

Fortunately, there is a simpler way. The map class provides a method called entryset (), which returns an object set after map. Entry instantiation. Next, the map. Entry class provides a getkey () method and a getvalue () method. Therefore, the above Code can be organized to be more logical. Example:

Set entries = map. entryset ();
If (entries! = NULL ){
Iterator = entries. iterator ();
While (iterator. hasnext ()){
Map. Entry entry = iterator. Next ();
Object key = entry. getkey ();
Object value = entry. getvalue ();
;....
}
}

Although a line of code is added, we omit many unnecessary "get" calls to map. At the same time, it provides developers with a class that maintains both the keyword and its corresponding value. Map. Entry also provides a setvalue () method, which can be used by programmers to modify values in map.

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.