Have you ever felt tired of getting a keyword from a map and then getting the corresponding value? 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 iterator = Keys.iterator ();
while (Iterator.hasnext ()) {
Object key = Iterator.next ();
Object value = Map.get (key);
; ....
;}
Then, there is a problem with this method. After getting the keyword from the map, it is tedious and time-consuming to return to the map each time to get a relative value.
Fortunately, there is a much simpler approach. The map class provides a method called EntrySet () that returns a map.entry set of objects that are instantiated. Next, the Map.entry class provides a Getkey () method and a GetValue () method, so the above code can be organized more logically. Examples are as follows:
Set entries = Map.entryset ();
if (entries!= null) {
iterator iterator = Entries.iterator ();
while (Iterator.hasnext ()) {
map.entry Entry =iterator.next ();
Object key = Entry.getkey ();
Object value = Entry.getvalue ();
; ....
}
Despite the addition of one line of code, we omit many "get" calls to map unnecessarily. At the same time, the developer is provided with a class that retains both the keyword and its corresponding value. Map.entry also provides a SetValue () method that programmers can use to modify values within a map.