Java Map and Map. Entry
Map is an interface in java, and Map. Entry is an internal interface of Map.
Map provides some common methods, such as keySet () and entrySet.
The Return Value of the keySet () method is the Set of key values in the Map. The return value of entrySet () is also a Set, which is of the Map. Entry type.
Map. Entry is an internal interface declared by Map. This interface is generic and defined as Entry. . It indicates an object in Map (a key-value Pair ). The Interface contains the getKey () and getValue methods.
From the above, we can conclude that the common methods for Traversing Map are as follows:
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 also a Traversal method, simply traversing the value, Map has a values method, the returned value Collection. You can also traverse the value by traversing the collection, as shown in figure
Map map = new HashMap ();
Collection c = map. values ();
Iterator iterator = c. iterator ();
While (iterator. hasNext ()){
Object value = iterator. next ();
}