# Map.Entry

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

# Entry

```
interface Entry<K : Any!, V : Any!>
```

|--------------------------------------------------------------------------------------------|
| [java.util.Map.Entry](https://developer.android.com/reference/kotlin/java/util/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.

An Entry maintains a connection to its underlying map if the Entry was obtained by iterating the [Map.entrySet](https://developer.android.com/reference/kotlin/java/util/Map#entrySet()) view of a map, either explicitly by using an [Iterator](https://developer.android.com/reference/kotlin/java/util/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](https://developer.android.com/reference/kotlin/java/util/Map.Entry#setValue(java.util.Map.Entry.V)) 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](https://developer.android.com/reference/kotlin/java/util/Collection#parallelStream()), [stream](https://developer.android.com/reference/kotlin/java/util/Collection#stream()), java.util.Set#spliterator methods, any of the java.util.Set#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](https://developer.android.com/reference/kotlin/java/util/Map.Entry#setValue(java.util.Map.Entry.V)) method.

In addition, an Entry may be obtained directly from a map, for example via calls to methods directly on the [NavigableMap](https://developer.android.com/reference/kotlin/java/util/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](https://developer.android.com/reference/kotlin/java/util/AbstractMap.SimpleEntry) or [AbstractMap.SimpleImmutableEntry](https://developer.android.com/reference/kotlin/java/util/AbstractMap.SimpleImmutableEntry) classes or from a call to the [Map.entry](https://developer.android.com/reference/kotlin/java/util/Map#entry(java.util.Map.entry.K,%20java.util.Map.entry.V)) or [Map.Entry.copyOf](https://developer.android.com/reference/kotlin/java/util/Map.Entry#copyOf(kotlin.collections.MutableMap.MutableEntry)) methods is not connected to any map.

## Summary

|                                                                                                                                                                                                                                                                                                                                            Public methods                                                                                                                                                                                                                                                                                                                                             ||
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| open static [Comparator](https://developer.android.com/reference/kotlin/java/util/Comparator)\<[MutableEntry](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/-mutable-entry/index.html)\<K, V\>!\> | [comparingByKey](https://developer.android.com/reference/kotlin/java/util/Map.Entry#comparingByKey())`()` Returns a comparator that compares [Map.Entry](https://developer.android.com/reference/kotlin/java/util/Map.Entry#) in natural order on key.                                                                                                                                                                                                              |
| open static [Comparator](https://developer.android.com/reference/kotlin/java/util/Comparator)\<[MutableEntry](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/-mutable-entry/index.html)\<K, V\>!\> | [comparingByKey](https://developer.android.com/reference/kotlin/java/util/Map.Entry#comparingByKey(java.util.Comparator))`(`cmp:` `[Comparator](https://developer.android.com/reference/kotlin/java/util/Comparator)<in` `K>`)` Returns a comparator that compares [Map.Entry](https://developer.android.com/reference/kotlin/java/util/Map.Entry#) by key using the given [Comparator](https://developer.android.com/reference/kotlin/java/util/Comparator).       |
| open static [Comparator](https://developer.android.com/reference/kotlin/java/util/Comparator)\<[MutableEntry](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/-mutable-entry/index.html)\<K, V\>!\> | [comparingByValue](https://developer.android.com/reference/kotlin/java/util/Map.Entry#comparingByValue())`()` Returns a comparator that compares [Map.Entry](https://developer.android.com/reference/kotlin/java/util/Map.Entry#) in natural order on value.                                                                                                                                                                                                        |
| open static [Comparator](https://developer.android.com/reference/kotlin/java/util/Comparator)\<[MutableEntry](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/-mutable-entry/index.html)\<K, V\>!\> | [comparingByValue](https://developer.android.com/reference/kotlin/java/util/Map.Entry#comparingByValue(java.util.Comparator))`(`cmp:` `[Comparator](https://developer.android.com/reference/kotlin/java/util/Comparator)<in` `V>`)` Returns a comparator that compares [Map.Entry](https://developer.android.com/reference/kotlin/java/util/Map.Entry#) by value using the given [Comparator](https://developer.android.com/reference/kotlin/java/util/Comparator). |
| open static [MutableEntry](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/-mutable-entry/index.html)\<K, V\>                                                                                       | [copyOf](https://developer.android.com/reference/kotlin/java/util/Map.Entry#copyOf(kotlin.collections.MutableMap.MutableEntry))`(`e:` `[MutableEntry](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/-mutable-entry/index.html)<out` `K,` `out` `V>`)` Returns a copy of the given `Map.Entry`.                                                                                                                                       |
| abstract K                                                                                                                                                                                                                       | [getKey](https://developer.android.com/reference/kotlin/java/util/Map.Entry#getKey())`()` Returns the key corresponding to this entry.                                                                                                                                                                                                                                                                                                                              |
| abstract V                                                                                                                                                                                                                       | [getValue](https://developer.android.com/reference/kotlin/java/util/Map.Entry#getValue())`()` Returns the value corresponding to this entry.                                                                                                                                                                                                                                                                                                                        |
| abstract V                                                                                                                                                                                                                       | [setValue](https://developer.android.com/reference/kotlin/java/util/Map.Entry#setValue(java.util.Map.Entry.V))`(`value:` `V`)` Replaces the value corresponding to this entry with the specified value (optional operation).                                                                                                                                                                                                                                        |

## Public methods

### comparingByKey

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open static fun <K : Comparable<K>!, V : Any!> comparingByKey(): Comparator<MutableEntry<K, V>!>
```

Returns a comparator that compares [Map.Entry](https://developer.android.com/reference/kotlin/java/util/Map.Entry#) in natural order on key.

The returned comparator is serializable and throws [NullPointerException](https://developer.android.com/reference/kotlin/java/lang/NullPointerException.html#) when comparing an entry with a null key.

|                                                        Parameters                                                        ||
|-------|-------------------------------------------------------------------------------------------------------------------|
| `<K>` | the [Comparable](https://developer.android.com/reference/kotlin/java/lang/Comparable.html#) type of then map keys |
| `<V>` | the type of the map values                                                                                        |

|                                                                                                                                                                          Return                                                                                                                                                                          ||
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------|
| [Comparator](https://developer.android.com/reference/kotlin/java/util/Comparator)<[MutableEntry](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/-mutable-entry/index.html)<K,` `V>!> | a comparator that compares [Map.Entry](https://developer.android.com/reference/kotlin/java/util/Map.Entry#) in natural order on key. |

**See Also**

- [java.lang.Comparable](https://developer.android.com/reference/kotlin/java/lang/Comparable.html#)  

### comparingByKey

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open static fun <K : Any!, V : Any!> comparingByKey(cmp: Comparator<in K>): Comparator<MutableEntry<K, V>!>
```

Returns a comparator that compares [Map.Entry](https://developer.android.com/reference/kotlin/java/util/Map.Entry#) by key using the given [Comparator](https://developer.android.com/reference/kotlin/java/util/Comparator).

The returned comparator is serializable if the specified comparator is also serializable.

|                                                                                         Parameters                                                                                          ||
|-------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `<K>` | the type of the map keys                                                                                                                                                             |
| `<V>` | the type of the map values                                                                                                                                                           |
| `cmp` | [Comparator](https://developer.android.com/reference/kotlin/java/util/Comparator)\<in K\>: the key [Comparator](https://developer.android.com/reference/kotlin/java/util/Comparator) |

|                                                                                                                                                                   Return                                                                                                                                                                    ||
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------|
| [Comparator](https://developer.android.com/reference/kotlin/java/util/Comparator)<[MutableEntry](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/-mutable-entry/index.html)<K,` `V>!> | a comparator that compares [Map.Entry](https://developer.android.com/reference/kotlin/java/util/Map.Entry#) by the key. |

### comparingByValue

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open static fun <K : Any!, V : Comparable<V>!> comparingByValue(): Comparator<MutableEntry<K, V>!>
```

Returns a comparator that compares [Map.Entry](https://developer.android.com/reference/kotlin/java/util/Map.Entry#) in natural order on value.

The returned comparator is serializable and throws [NullPointerException](https://developer.android.com/reference/kotlin/java/lang/NullPointerException.html#) when comparing an entry with null values.

|                                                        Parameters                                                         ||
|-------|--------------------------------------------------------------------------------------------------------------------|
| `<K>` | the type of the map keys                                                                                           |
| `<V>` | the [Comparable](https://developer.android.com/reference/kotlin/java/lang/Comparable.html#) type of the map values |

|                                                                                                                                                                           Return                                                                                                                                                                           ||
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------|
| [Comparator](https://developer.android.com/reference/kotlin/java/util/Comparator)<[MutableEntry](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/-mutable-entry/index.html)<K,` `V>!> | a comparator that compares [Map.Entry](https://developer.android.com/reference/kotlin/java/util/Map.Entry#) in natural order on value. |

**See Also**

- [java.lang.Comparable](https://developer.android.com/reference/kotlin/java/lang/Comparable.html#)  

### comparingByValue

Added in [API level 24](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open static fun <K : Any!, V : Any!> comparingByValue(cmp: Comparator<in V>): Comparator<MutableEntry<K, V>!>
```

Returns a comparator that compares [Map.Entry](https://developer.android.com/reference/kotlin/java/util/Map.Entry#) by value using the given [Comparator](https://developer.android.com/reference/kotlin/java/util/Comparator).

The returned comparator is serializable if the specified comparator is also serializable.

|                                                                                          Parameters                                                                                           ||
|-------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `<K>` | the type of the map keys                                                                                                                                                               |
| `<V>` | the type of the map values                                                                                                                                                             |
| `cmp` | [Comparator](https://developer.android.com/reference/kotlin/java/util/Comparator)\<in V\>: the value [Comparator](https://developer.android.com/reference/kotlin/java/util/Comparator) |

|                                                                                                                                                                    Return                                                                                                                                                                     ||
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------|
| [Comparator](https://developer.android.com/reference/kotlin/java/util/Comparator)<[MutableEntry](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/-mutable-entry/index.html)<K,` `V>!> | a comparator that compares [Map.Entry](https://developer.android.com/reference/kotlin/java/util/Map.Entry#) by the value. |

### copyOf

Added in [API level 35](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
open static fun <K : Any!, V : Any!> copyOf(e: MutableEntry<out K, out V>): MutableEntry<K, V>
```

Returns a copy of the given `Map.Entry`. The returned instance is not associated with any map. The returned instance has the same characteristics as instances returned by the [Map::entry](https://developer.android.com/reference/kotlin/java/util/Map#entry(java.util.Map.entry.K,%20java.util.Map.entry.V)) method.

|                                                                              Parameters                                                                               ||
|-------|----------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `<K>` | the type of the key                                                                                                                                            |
| `<V>` | the type of the value                                                                                                                                          |
| `e`   | [MutableEntry](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/-mutable-entry/index.html)\<out K, out V\>: the entry to be copied |

|                                                                                Return                                                                                ||
|--------------------------------------------------------------------------------------------------------------------------------|--------------------------------------|
| [MutableEntry](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/-mutable-entry/index.html)<K,` `V> | a map entry equal to the given entry |

|                                       Exceptions                                        ||
|----------------------------------|-------------------------------------------------------|
| `java.lang.NullPointerException` | if e is null or if either of its key or value is null |

### getKey

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
abstract fun getKey(): K
```

Returns the key corresponding to this entry.

|                 Return                 ||
|---|-------------------------------------|
| K | the key corresponding to this entry |

|                                                                        Exceptions                                                                         ||
|-----------------------------------|------------------------------------------------------------------------------------------------------------------------|
| `java.lang.IllegalStateException` | implementations may, but are not required to, throw this exception if the entry has been removed from the backing map. |

### getValue

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
abstract fun getValue(): V
```

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.

|                  Return                  ||
|---|---------------------------------------|
| V | the value corresponding to this entry |

|                                                                        Exceptions                                                                         ||
|-----------------------------------|------------------------------------------------------------------------------------------------------------------------|
| `java.lang.IllegalStateException` | implementations may, but are not required to, throw this exception if the entry has been removed from the backing map. |

### setValue

Added in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)  

```
abstract fun setValue(value: V): V
```

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).

|                    Parameters                    ||
|---------|-----------------------------------------|
| `value` | V: new value to be stored in this entry |

|                 Return                  ||
|---|--------------------------------------|
| V | old value corresponding to the entry |

|                                                                            Exceptions                                                                             ||
|-------------------------------------------|------------------------------------------------------------------------------------------------------------------------|
| `java.lang.UnsupportedOperationException` | if the `put` operation is not supported by the backing map                                                             |
| `java.lang.ClassCastException`            | if the class of the specified value prevents it from being stored in the backing map                                   |
| `java.lang.NullPointerException`          | if the backing map does not permit null values, and the specified value is null                                        |
| `java.lang.IllegalArgumentException`      | if some property of this value prevents it from being stored in the backing map                                        |
| `java.lang.IllegalStateException`         | implementations may, but are not required to, throw this exception if the entry has been removed from the backing map. |