The map is an interface in Java, and Map.entry is an internal interface to the map.
Map provides a number of common methods, such as keyset (), EntrySet (), and so on.
The KeySet () method return value is a collection of key values in map; The return value of EntrySet () also returns a set collection of type Map.entry.
Map.entry is an internal interface to the map declaration, which is generic and defined as entry<k,v>. It represents an entity (a Key-value pair) in a map. The interface has a getkey (), GetValue method.
From the above can be drawn, the common method of traversing map:
1. Map map = new HashMap ();
Irerator iterator = Map.entryset (). iterator ();
while (Iterator.hasnext ()) {
Map.entry Entry = Iterator.next ();
Object key = Entry.getkey ();
//
}
2.Map Map = new HashMap ();
Set keyset= Map.keyset ();
Irerator iterator = Keyset.iterator;
while (Iterator.hasnext ()) {
Object key = Iterator.next ();
Object value = Map.get (key);
//
}
In addition, there is a traversal method, simply traversing the value values, map has a values method, which returns the collection collection of value. By traversing collection, you can also traverse value, such as
Map map = new HashMap ();
Collection C = map.values ();
Iterator Iterator = C.iterator ();
while (Iterator.hasnext ()) {
Object value = Iterator.next ();
}
Because the map class does not inherit the Iterable interface, it cannot be traversed directly through the Map.iterator (List,set is the implementation of this interface, so it can be traversed directly), so it can only be converted to set type, with the EntrySet () method, The value of each element in the set is a key-value pair in the map, which is map.entry<k,v>, and then it can be traversed.
It's basically the time to traverse the map.
Java map and Map.entry detailed