Introduce associateWith and associateWithTo functions
#KT-13814
This commit is contained in:
@@ -1059,6 +1059,36 @@ public inline fun <T, K, V, M : MutableMap<in K, in V>> Iterable<T>.associateTo(
|
||||
return destination
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [Map] where keys are elements from the given collection and values are
|
||||
* produced by the [valueSelector] function applied to each element.
|
||||
*
|
||||
* If any two elements are equal, the last one gets added to the map.
|
||||
*
|
||||
* The returned map preserves the entry iteration order of the original collection.
|
||||
*
|
||||
* @sample samples.collections.Collections.Transformations.associateWith
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
public inline fun <K, V> Iterable<K>.associateWith(valueSelector: (K) -> V): Map<K, V> {
|
||||
val result = LinkedHashMap<K, V>(mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16))
|
||||
return associateWithTo(result, valueSelector)
|
||||
}
|
||||
|
||||
/**
|
||||
* Populates and returns the [destination] mutable map with key-value pairs for each element of the given collection,
|
||||
* where key is the element itself and value is provided by the [valueSelector] function applied to that key.
|
||||
*
|
||||
* If any two elements are equal, the last one overwrites the former value in the map.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
public inline fun <K, V, M : MutableMap<in K, in V>> Iterable<K>.associateWithTo(destination: M, valueSelector: (K) -> V): M {
|
||||
for (element in this) {
|
||||
destination.put(element, valueSelector(element))
|
||||
}
|
||||
return destination
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends all elements to the given [destination] collection.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user