Rename toMap { selector } to toMapBy.
#KT-6657
This commit is contained in:
@@ -5514,121 +5514,49 @@ public fun ShortArray.toList(): List<Short> {
|
||||
return this.toArrayList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Map containing the values from the given collection indexed by [selector].
|
||||
* 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)"))
|
||||
public inline fun <T, K> Array<out T>.toMap(selector: (T) -> K): Map<K, T> {
|
||||
val capacity = (size()/.75f) + 1
|
||||
val result = LinkedHashMap<K, T>(Math.max(capacity.toInt(), 16))
|
||||
for (element in this) {
|
||||
result.put(selector(element), element)
|
||||
}
|
||||
return result
|
||||
return toMapBy(selector)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Map containing the values from the given collection indexed by [selector].
|
||||
* 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)"))
|
||||
public inline fun <K> BooleanArray.toMap(selector: (Boolean) -> K): Map<K, Boolean> {
|
||||
val capacity = (size()/.75f) + 1
|
||||
val result = LinkedHashMap<K, Boolean>(Math.max(capacity.toInt(), 16))
|
||||
for (element in this) {
|
||||
result.put(selector(element), element)
|
||||
}
|
||||
return result
|
||||
return toMapBy(selector)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Map containing the values from the given collection indexed by [selector].
|
||||
* 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)"))
|
||||
public inline fun <K> ByteArray.toMap(selector: (Byte) -> K): Map<K, Byte> {
|
||||
val capacity = (size()/.75f) + 1
|
||||
val result = LinkedHashMap<K, Byte>(Math.max(capacity.toInt(), 16))
|
||||
for (element in this) {
|
||||
result.put(selector(element), element)
|
||||
}
|
||||
return result
|
||||
return toMapBy(selector)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Map containing the values from the given collection indexed by [selector].
|
||||
* 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)"))
|
||||
public inline fun <K> CharArray.toMap(selector: (Char) -> K): Map<K, Char> {
|
||||
val capacity = (size()/.75f) + 1
|
||||
val result = LinkedHashMap<K, Char>(Math.max(capacity.toInt(), 16))
|
||||
for (element in this) {
|
||||
result.put(selector(element), element)
|
||||
}
|
||||
return result
|
||||
return toMapBy(selector)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Map containing the values from the given collection indexed by [selector].
|
||||
* 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)"))
|
||||
public inline fun <K> DoubleArray.toMap(selector: (Double) -> K): Map<K, Double> {
|
||||
val capacity = (size()/.75f) + 1
|
||||
val result = LinkedHashMap<K, Double>(Math.max(capacity.toInt(), 16))
|
||||
for (element in this) {
|
||||
result.put(selector(element), element)
|
||||
}
|
||||
return result
|
||||
return toMapBy(selector)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Map containing the values from the given collection indexed by [selector].
|
||||
* 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)"))
|
||||
public inline fun <K> FloatArray.toMap(selector: (Float) -> K): Map<K, Float> {
|
||||
val capacity = (size()/.75f) + 1
|
||||
val result = LinkedHashMap<K, Float>(Math.max(capacity.toInt(), 16))
|
||||
for (element in this) {
|
||||
result.put(selector(element), element)
|
||||
}
|
||||
return result
|
||||
return toMapBy(selector)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Map containing the values from the given collection indexed by [selector].
|
||||
* 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)"))
|
||||
public inline fun <K> IntArray.toMap(selector: (Int) -> K): Map<K, Int> {
|
||||
val capacity = (size()/.75f) + 1
|
||||
val result = LinkedHashMap<K, Int>(Math.max(capacity.toInt(), 16))
|
||||
for (element in this) {
|
||||
result.put(selector(element), element)
|
||||
}
|
||||
return result
|
||||
return toMapBy(selector)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Map containing the values from the given collection indexed by [selector].
|
||||
* 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)"))
|
||||
public inline fun <K> LongArray.toMap(selector: (Long) -> K): Map<K, Long> {
|
||||
val capacity = (size()/.75f) + 1
|
||||
val result = LinkedHashMap<K, Long>(Math.max(capacity.toInt(), 16))
|
||||
for (element in this) {
|
||||
result.put(selector(element), element)
|
||||
}
|
||||
return result
|
||||
return toMapBy(selector)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Map containing the values from the given collection indexed by [selector].
|
||||
* 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)"))
|
||||
public inline fun <K> ShortArray.toMap(selector: (Short) -> K): Map<K, Short> {
|
||||
val capacity = (size()/.75f) + 1
|
||||
val result = LinkedHashMap<K, Short>(Math.max(capacity.toInt(), 16))
|
||||
for (element in this) {
|
||||
result.put(selector(element), element)
|
||||
}
|
||||
return result
|
||||
return toMapBy(selector)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -5748,6 +5676,123 @@ public inline fun <K, V> ShortArray.toMap(selector: (Short) -> K, transform: (Sh
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Map containing the values from the given collection indexed by [selector].
|
||||
* 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> Array<out T>.toMapBy(selector: (T) -> K): Map<K, T> {
|
||||
val capacity = (size()/.75f) + 1
|
||||
val result = LinkedHashMap<K, T>(Math.max(capacity.toInt(), 16))
|
||||
for (element in this) {
|
||||
result.put(selector(element), element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Map containing the values from the given collection indexed by [selector].
|
||||
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
|
||||
*/
|
||||
public inline fun <K> BooleanArray.toMapBy(selector: (Boolean) -> K): Map<K, Boolean> {
|
||||
val capacity = (size()/.75f) + 1
|
||||
val result = LinkedHashMap<K, Boolean>(Math.max(capacity.toInt(), 16))
|
||||
for (element in this) {
|
||||
result.put(selector(element), element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Map containing the values from the given collection indexed by [selector].
|
||||
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
|
||||
*/
|
||||
public inline fun <K> ByteArray.toMapBy(selector: (Byte) -> K): Map<K, Byte> {
|
||||
val capacity = (size()/.75f) + 1
|
||||
val result = LinkedHashMap<K, Byte>(Math.max(capacity.toInt(), 16))
|
||||
for (element in this) {
|
||||
result.put(selector(element), element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Map containing the values from the given collection indexed by [selector].
|
||||
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
|
||||
*/
|
||||
public inline fun <K> CharArray.toMapBy(selector: (Char) -> K): Map<K, Char> {
|
||||
val capacity = (size()/.75f) + 1
|
||||
val result = LinkedHashMap<K, Char>(Math.max(capacity.toInt(), 16))
|
||||
for (element in this) {
|
||||
result.put(selector(element), element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Map containing the values from the given collection indexed by [selector].
|
||||
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
|
||||
*/
|
||||
public inline fun <K> DoubleArray.toMapBy(selector: (Double) -> K): Map<K, Double> {
|
||||
val capacity = (size()/.75f) + 1
|
||||
val result = LinkedHashMap<K, Double>(Math.max(capacity.toInt(), 16))
|
||||
for (element in this) {
|
||||
result.put(selector(element), element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Map containing the values from the given collection indexed by [selector].
|
||||
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
|
||||
*/
|
||||
public inline fun <K> FloatArray.toMapBy(selector: (Float) -> K): Map<K, Float> {
|
||||
val capacity = (size()/.75f) + 1
|
||||
val result = LinkedHashMap<K, Float>(Math.max(capacity.toInt(), 16))
|
||||
for (element in this) {
|
||||
result.put(selector(element), element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Map containing the values from the given collection indexed by [selector].
|
||||
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
|
||||
*/
|
||||
public inline fun <K> IntArray.toMapBy(selector: (Int) -> K): Map<K, Int> {
|
||||
val capacity = (size()/.75f) + 1
|
||||
val result = LinkedHashMap<K, Int>(Math.max(capacity.toInt(), 16))
|
||||
for (element in this) {
|
||||
result.put(selector(element), element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Map containing the values from the given collection indexed by [selector].
|
||||
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
|
||||
*/
|
||||
public inline fun <K> LongArray.toMapBy(selector: (Long) -> K): Map<K, Long> {
|
||||
val capacity = (size()/.75f) + 1
|
||||
val result = LinkedHashMap<K, Long>(Math.max(capacity.toInt(), 16))
|
||||
for (element in this) {
|
||||
result.put(selector(element), element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Map containing the values from the given collection indexed by [selector].
|
||||
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
|
||||
*/
|
||||
public inline fun <K> ShortArray.toMapBy(selector: (Short) -> K): Map<K, Short> {
|
||||
val capacity = (size()/.75f) + 1
|
||||
val result = LinkedHashMap<K, Short>(Math.max(capacity.toInt(), 16))
|
||||
for (element in this) {
|
||||
result.put(selector(element), element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [Set] of all elements.
|
||||
*/
|
||||
|
||||
@@ -894,17 +894,9 @@ public fun <T> Iterable<T>.toList(): List<T> {
|
||||
return this.toArrayList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Map containing the values from the given collection indexed by [selector].
|
||||
* 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)"))
|
||||
public inline fun <T, K> Iterable<T>.toMap(selector: (T) -> K): Map<K, T> {
|
||||
val capacity = (collectionSizeOrDefault(10)/.75f) + 1
|
||||
val result = LinkedHashMap<K, T>(Math.max(capacity.toInt(), 16))
|
||||
for (element in this) {
|
||||
result.put(selector(element), element)
|
||||
}
|
||||
return result
|
||||
return toMapBy(selector)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -920,6 +912,19 @@ public inline fun <T, K, V> Iterable<T>.toMap(selector: (T) -> K, transform: (T)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Map containing the values from the given collection indexed by [selector].
|
||||
* 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> Iterable<T>.toMapBy(selector: (T) -> K): Map<K, T> {
|
||||
val capacity = (collectionSizeOrDefault(10)/.75f) + 1
|
||||
val result = LinkedHashMap<K, T>(Math.max(capacity.toInt(), 16))
|
||||
for (element in this) {
|
||||
result.put(selector(element), element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [Set] of all elements.
|
||||
*/
|
||||
|
||||
@@ -441,16 +441,9 @@ public fun <T> Sequence<T>.toList(): List<T> {
|
||||
return this.toArrayList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Map containing the values from the given collection indexed by [selector].
|
||||
* 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)"))
|
||||
public inline fun <T, K> Sequence<T>.toMap(selector: (T) -> K): Map<K, T> {
|
||||
val result = LinkedHashMap<K, T>()
|
||||
for (element in this) {
|
||||
result.put(selector(element), element)
|
||||
}
|
||||
return result
|
||||
return toMapBy(selector)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -465,6 +458,18 @@ public inline fun <T, K, V> Sequence<T>.toMap(selector: (T) -> K, transform: (T)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Map containing the values from the given collection indexed by [selector].
|
||||
* 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> Sequence<T>.toMapBy(selector: (T) -> K): Map<K, T> {
|
||||
val result = LinkedHashMap<K, T>()
|
||||
for (element in this) {
|
||||
result.put(selector(element), element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [Set] of all elements.
|
||||
*/
|
||||
|
||||
@@ -386,17 +386,9 @@ public fun String.toList(): List<Char> {
|
||||
return this.toArrayList()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Map containing the values from the given collection indexed by [selector].
|
||||
* 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)"))
|
||||
public inline fun <K> String.toMap(selector: (Char) -> K): Map<K, Char> {
|
||||
val capacity = (length()/.75f) + 1
|
||||
val result = LinkedHashMap<K, Char>(Math.max(capacity.toInt(), 16))
|
||||
for (element in this) {
|
||||
result.put(selector(element), element)
|
||||
}
|
||||
return result
|
||||
return toMapBy(selector)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -412,6 +404,19 @@ public inline fun <K, V> String.toMap(selector: (Char) -> K, transform: (Char) -
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Map containing the values from the given collection indexed by [selector].
|
||||
* If any two elements would have the same key returned by [selector] the last one gets added to the map.
|
||||
*/
|
||||
public inline fun <K> String.toMapBy(selector: (Char) -> K): Map<K, Char> {
|
||||
val capacity = (length()/.75f) + 1
|
||||
val result = LinkedHashMap<K, Char>(Math.max(capacity.toInt(), 16))
|
||||
for (element in this) {
|
||||
result.put(selector(element), element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [Set] of all elements.
|
||||
*/
|
||||
|
||||
@@ -160,7 +160,7 @@ class MapTest {
|
||||
}
|
||||
|
||||
@test fun createWithSelector() {
|
||||
val map = listOf("a", "bb", "ccc").toMap { it.length() }
|
||||
val map = listOf("a", "bb", "ccc").toMapBy { it.length() }
|
||||
assertEquals(3, map.size())
|
||||
assertEquals("a", map.get(1))
|
||||
assertEquals("bb", map.get(2))
|
||||
@@ -168,7 +168,7 @@ class MapTest {
|
||||
}
|
||||
|
||||
@test fun createWithSelectorAndOverwrite() {
|
||||
val map = listOf("aa", "bb", "ccc").toMap { it.length() }
|
||||
val map = listOf("aa", "bb", "ccc").toMapBy { it.length() }
|
||||
assertEquals(2, map.size())
|
||||
assertEquals("bb", map.get(2))
|
||||
assertEquals("ccc", map.get(3))
|
||||
|
||||
@@ -94,6 +94,16 @@ fun snapshots(): List<GenericFunction> {
|
||||
}
|
||||
|
||||
templates add f("toMap(selector: (T) -> K)") {
|
||||
inline(true)
|
||||
include(Strings)
|
||||
typeParam("K")
|
||||
returns("Map<K, T>")
|
||||
deprecate("Use toMapBy instead.")
|
||||
deprecateReplacement("toMapBy(selector)")
|
||||
body("return ${deprecateReplacement.default}")
|
||||
}
|
||||
|
||||
templates add f("toMapBy(selector: (T) -> K)") {
|
||||
inline(true)
|
||||
typeParam("K")
|
||||
doc {
|
||||
|
||||
Reference in New Issue
Block a user