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.