Module java.base
Package java.util

Interface Map.Entry<K,V>

Type Parameters:
K - the type of the key
V - the type of the value
All Known Implementing Classes:
AbstractMap.SimpleEntry, AbstractMap.SimpleImmutableEntry
Enclosing interface:
Map<K,V>

public static interface Map.Entry<K,V>
A map entry (key-value pair). The Entry may be unmodifiable, or the value may be modifiable if the optional setValue method is implemented. The Entry may be independent of any map, or it may represent an entry of the entry-set view of a map.

An Entry maintains a connection to its underlying map if the Entry was obtained by iterating the Map.entrySet() view of a map, either explicitly by using an Iterator or implicitly via the enhanced for statement. This connection to the backing map is valid only during iteration of the entry-set view. During the iteration, if supported by the backing map, a change to an Entry's value via the setValue method will be visible in the backing map. The behavior of such an Entry is undefined outside of iteration of the map's entry-set view. It is also undefined if the backing map has been modified after the Entry was returned by the iterator, except through the setValue method. In addition, a change to the value of a mapping in the backing map might or might not be visible in the corresponding Entry of the entry-set view.

An Entry may also be obtained from a map's entry-set view by other means, for example, using the parallelStream, stream, spliterator methods, any of the toArray overloads, or by copying the entry-set view into another collection. It is unspecified whether the obtained Entry instances are connected to the underlying map, whether changes to such an Entry will affect the underlying the map and vice-versa, and whether such an Entry supports the optional setValue method.

In addition, an Entry may be obtained directly from a map, for example via calls to methods directly on the NavigableMap interface. An entry thus obtained is generally not connected to the map and is an unmodifiable snapshot of the mapping as of the time of the call. Such an Entry also does not generally support the setValue method.

An Entry obtained by direct construction of the AbstractMap.SimpleEntry or AbstractMap.SimpleImmutableEntry classes or from a call to the Map.entry or Map.Entry.copyOf methods is not connected to any map.

API Note:
The exact behavior of Entry instances obtained from a map's entry-set view other than via iteration varies across different map implementations; some are connected to the backing map, and some are not. To guarantee that an Entry is disconnected from its backing map, use the copyOf method. For example, the following creates a snapshot of a map's entries that is guaranteed not to change even if the original map is modified:
 
 var entries = map.entrySet().stream().map(Map.Entry::copyOf).toList()
 
Since:
1.2
See Also: