Interface Map.Entry
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.
Instances of the Map.Entry interface may be obtained by iterating the entry-set view of a map. These instances maintain a connection to the original, backing map. This connection to the backing map is valid only for the duration of iteration over the entry-set view. During iteration of the entry-set view, if supported by the backing map, a change to a Map.Entry ‘s value via the setValue method will be visible in the backing map. The behavior of such a Map.Entry instance 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 Map.Entry was returned by the iterator, except through the Map.Entry.setValue method. In particular, a change to the value of a mapping in the backing map might or might not be visible in the corresponding Map.Entry element of the entry-set view.
API Note: It is possible to create a Map.Entry instance that is disconnected from a backing map by using 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()
Java map entry key
A map entry (key-value pair). The Map.entrySet method returns a collection-view of the map, whose elements are of this class. The only way to obtain a reference to a map entry is from the iterator of this collection-view. These Map.Entry objects are valid only for the duration of the iteration; more formally, the behavior of a map entry is undefined if the backing map has been modified after the entry was returned by the iterator, except through the setValue operation on the map entry.
Method Summary
Method Detail
getKey
getValue
Returns the value corresponding to this entry. If the mapping has been removed from the backing map (by the iterator’s remove operation), the results of this call are undefined.
setValue
Replaces the value corresponding to this entry with the specified value (optional operation). (Writes through to the map.) The behavior of this call is undefined if the mapping has already been removed from the map (by the iterator’s remove operation).
equals
Compares the specified object with this entry for equality. Returns true if the given object is also a map entry and the two entries represent the same mapping. More formally, two entries e1 and e2 represent the same mapping if
(e1.getKey()==null ? e2.getKey()==null : e1.getKey().equals(e2.getKey())) && (e1.getValue()==null ? e2.getValue()==null : e1.getValue().equals(e2.getValue()))
This ensures that the equals method works properly across different implementations of the Map.Entry interface.
hashCode
(e.getKey()==null ? 0 : e.getKey().hashCode()) ^ (e.getValue()==null ? 0 : e.getValue().hashCode())
This ensures that e1.equals(e2) implies that e1.hashCode()==e2.hashCode() for any two Entries e1 and e2 , as required by the general contract of Object.hashCode .
comparingByKey
static Comparable,V> ComparatorMap.Entry> comparingByKey()
Returns a comparator that compares Map.Entry in natural order on key. The returned comparator is serializable and throws NullPointerException when comparing an entry with a null key.
comparingByValue
static Comparable> ComparatorMap.Entry> comparingByValue()
Returns a comparator that compares Map.Entry in natural order on value. The returned comparator is serializable and throws NullPointerException when comparing an entry with null values.
comparingByKey
static ComparatorMap.Entry> comparingByKey(Comparator cmp)
Returns a comparator that compares Map.Entry by key using the given Comparator . The returned comparator is serializable if the specified comparator is also serializable.
comparingByValue
static ComparatorMap.Entry> comparingByValue(Comparator cmp)
Returns a comparator that compares Map.Entry by value using the given Comparator . The returned comparator is serializable if the specified comparator is also serializable.
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.
Interface Map.Entry
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.
Instances of the Map.Entry interface may be obtained by iterating the entry-set view of a map. These instances maintain a connection to the original, backing map. This connection to the backing map is valid only for the duration of iteration over the entry-set view. During iteration of the entry-set view, if supported by the backing map, a change to a Map.Entry ‘s value via the setValue method will be visible in the backing map. The behavior of such a Map.Entry instance 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 Map.Entry was returned by the iterator, except through the Map.Entry.setValue method. In particular, a change to the value of a mapping in the backing map might or might not be visible in the corresponding Map.Entry element of the entry-set view.
API Note: It is possible to create a Map.Entry instance that is disconnected from a backing map by using 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()
Java map entry key
A map entry (key-value pair). The Map.entrySet method returns a collection-view of the map, whose elements are of this class. The only way to obtain a reference to a map entry is from the iterator of this collection-view. These Map.Entry objects are valid only for the duration of the iteration; more formally, the behavior of a map entry is undefined if the backing map has been modified after the entry was returned by the iterator, except through the setValue operation on the map entry.
Method Summary
Method Detail
getKey
getValue
Returns the value corresponding to this entry. If the mapping has been removed from the backing map (by the iterator’s remove operation), the results of this call are undefined.
setValue
Replaces the value corresponding to this entry with the specified value (optional operation). (Writes through to the map.) The behavior of this call is undefined if the mapping has already been removed from the map (by the iterator’s remove operation).
equals
Compares the specified object with this entry for equality. Returns true if the given object is also a map entry and the two entries represent the same mapping. More formally, two entries e1 and e2 represent the same mapping if
(e1.getKey()==null ? e2.getKey()==null : e1.getKey().equals(e2.getKey())) && (e1.getValue()==null ? e2.getValue()==null : e1.getValue().equals(e2.getValue()))
This ensures that the equals method works properly across different implementations of the Map.Entry interface.
hashCode
(e.getKey()==null ? 0 : e.getKey().hashCode()) ^ (e.getValue()==null ? 0 : e.getValue().hashCode())
This ensures that e1.equals(e2) implies that e1.hashCode()==e2.hashCode() for any two Entries e1 and e2, as required by the general contract of Object.hashCode.
comparingByKey
static Comparable,V> ComparatorMap.Entry> comparingByKey()
Returns a comparator that compares Map.Entry in natural order on key. The returned comparator is serializable and throws NullPointerException when comparing an entry with a null key.
comparingByValue
static Comparable> ComparatorMap.Entry> comparingByValue()
Returns a comparator that compares Map.Entry in natural order on value. The returned comparator is serializable and throws NullPointerException when comparing an entry with null values.
comparingByKey
static ComparatorMap.Entry> comparingByKey(Comparator cmp)
Returns a comparator that compares Map.Entry by key using the given Comparator . The returned comparator is serializable if the specified comparator is also serializable.
comparingByValue
static ComparatorMap.Entry> comparingByValue(Comparator cmp)
Returns a comparator that compares Map.Entry by value using the given Comparator . The returned comparator is serializable if the specified comparator is also serializable.
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2023, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.