Rename toMap with selector and transform to toMapBy

This commit is contained in:
Ilya Gorbunov
2015-12-17 08:30:16 +03:00
parent 544bc9a70c
commit b5e637bed5
5 changed files with 199 additions and 79 deletions
+15 -6
View File
@@ -1073,13 +1073,9 @@ public inline fun <T, K> Iterable<T>.toMap(selector: (T) -> K): Map<K, T> {
* Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to elements of the given collection.
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/
@Deprecated("Use toMapBy instead.", ReplaceWith("toMapBy(selector, transform)"))
public inline fun <T, K, V> Iterable<T>.toMap(selector: (T) -> K, transform: (T) -> V): Map<K, V> {
val capacity = (collectionSizeOrDefault(10)/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), transform(element))
}
return result
return toMapBy(selector, transform)
}
/**
@@ -1110,6 +1106,19 @@ public inline fun <T, K> Iterable<T>.toMapBy(selector: (T) -> K): Map<K, T> {
return result
}
/**
* Returns a [Map] containing the values provided by [transform] and indexed by [selector] functions applied to elements of the given collection.
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
*/
public inline fun <T, K, V> Iterable<T>.toMapBy(selector: (T) -> K, transform: (T) -> V): Map<K, V> {
val capacity = (collectionSizeOrDefault(10)/.75f) + 1
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
for (element in this) {
result.put(selector(element), transform(element))
}
return result
}
/**
* Returns a [Set] of all elements.
*/