Introduce toMap with key-value pair selector.
#KT-6657
This commit is contained in:
@@ -6004,6 +6004,132 @@ public inline fun <K, V> ShortArray.toMap(selector: (Short) -> K, transform: (Sh
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array.
|
||||||
|
* If any of two pairs would have the same key the last one gets added to the map.
|
||||||
|
*/
|
||||||
|
@kotlin.jvm.JvmName("toMapOfPairs")
|
||||||
|
public inline fun <T, K, V> Array<out T>.toMap(transform: (T) -> Pair<K, V>): Map<K, V> {
|
||||||
|
val capacity = (size/.75f) + 1
|
||||||
|
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
|
||||||
|
for (element in this) {
|
||||||
|
result += transform(element)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array.
|
||||||
|
* If any of two pairs would have the same key the last one gets added to the map.
|
||||||
|
*/
|
||||||
|
@kotlin.jvm.JvmName("toMapOfPairs")
|
||||||
|
public inline fun <K, V> BooleanArray.toMap(transform: (Boolean) -> Pair<K, V>): Map<K, V> {
|
||||||
|
val capacity = (size/.75f) + 1
|
||||||
|
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
|
||||||
|
for (element in this) {
|
||||||
|
result += transform(element)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array.
|
||||||
|
* If any of two pairs would have the same key the last one gets added to the map.
|
||||||
|
*/
|
||||||
|
@kotlin.jvm.JvmName("toMapOfPairs")
|
||||||
|
public inline fun <K, V> ByteArray.toMap(transform: (Byte) -> Pair<K, V>): Map<K, V> {
|
||||||
|
val capacity = (size/.75f) + 1
|
||||||
|
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
|
||||||
|
for (element in this) {
|
||||||
|
result += transform(element)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array.
|
||||||
|
* If any of two pairs would have the same key the last one gets added to the map.
|
||||||
|
*/
|
||||||
|
@kotlin.jvm.JvmName("toMapOfPairs")
|
||||||
|
public inline fun <K, V> CharArray.toMap(transform: (Char) -> Pair<K, V>): Map<K, V> {
|
||||||
|
val capacity = (size/.75f) + 1
|
||||||
|
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
|
||||||
|
for (element in this) {
|
||||||
|
result += transform(element)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array.
|
||||||
|
* If any of two pairs would have the same key the last one gets added to the map.
|
||||||
|
*/
|
||||||
|
@kotlin.jvm.JvmName("toMapOfPairs")
|
||||||
|
public inline fun <K, V> DoubleArray.toMap(transform: (Double) -> Pair<K, V>): Map<K, V> {
|
||||||
|
val capacity = (size/.75f) + 1
|
||||||
|
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
|
||||||
|
for (element in this) {
|
||||||
|
result += transform(element)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array.
|
||||||
|
* If any of two pairs would have the same key the last one gets added to the map.
|
||||||
|
*/
|
||||||
|
@kotlin.jvm.JvmName("toMapOfPairs")
|
||||||
|
public inline fun <K, V> FloatArray.toMap(transform: (Float) -> Pair<K, V>): Map<K, V> {
|
||||||
|
val capacity = (size/.75f) + 1
|
||||||
|
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
|
||||||
|
for (element in this) {
|
||||||
|
result += transform(element)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array.
|
||||||
|
* If any of two pairs would have the same key the last one gets added to the map.
|
||||||
|
*/
|
||||||
|
@kotlin.jvm.JvmName("toMapOfPairs")
|
||||||
|
public inline fun <K, V> IntArray.toMap(transform: (Int) -> Pair<K, V>): Map<K, V> {
|
||||||
|
val capacity = (size/.75f) + 1
|
||||||
|
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
|
||||||
|
for (element in this) {
|
||||||
|
result += transform(element)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array.
|
||||||
|
* If any of two pairs would have the same key the last one gets added to the map.
|
||||||
|
*/
|
||||||
|
@kotlin.jvm.JvmName("toMapOfPairs")
|
||||||
|
public inline fun <K, V> LongArray.toMap(transform: (Long) -> Pair<K, V>): Map<K, V> {
|
||||||
|
val capacity = (size/.75f) + 1
|
||||||
|
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
|
||||||
|
for (element in this) {
|
||||||
|
result += transform(element)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given array.
|
||||||
|
* If any of two pairs would have the same key the last one gets added to the map.
|
||||||
|
*/
|
||||||
|
@kotlin.jvm.JvmName("toMapOfPairs")
|
||||||
|
public inline fun <K, V> ShortArray.toMap(transform: (Short) -> Pair<K, V>): Map<K, V> {
|
||||||
|
val capacity = (size/.75f) + 1
|
||||||
|
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
|
||||||
|
for (element in this) {
|
||||||
|
result += transform(element)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a [Map] containing the elements from the given array indexed by the key
|
* Returns a [Map] containing the elements from the given array indexed by the key
|
||||||
* returned from [selector] function applied to each element.
|
* returned from [selector] function applied to each element.
|
||||||
|
|||||||
@@ -1082,6 +1082,20 @@ public inline fun <T, K, V> Iterable<T>.toMap(selector: (T) -> K, transform: (T)
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given collection.
|
||||||
|
* If any of two pairs would have the same key the last one gets added to the map.
|
||||||
|
*/
|
||||||
|
@kotlin.jvm.JvmName("toMapOfPairs")
|
||||||
|
public inline fun <T, K, V> Iterable<T>.toMap(transform: (T) -> Pair<K, 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 += transform(element)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a [Map] containing the elements from the given collection indexed by the key
|
* Returns a [Map] containing the elements from the given collection indexed by the key
|
||||||
* returned from [selector] function applied to each element.
|
* returned from [selector] function applied to each element.
|
||||||
|
|||||||
@@ -539,6 +539,19 @@ public inline fun <T, K, V> Sequence<T>.toMap(selector: (T) -> K, transform: (T)
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a [Map] containing key-value pairs provided by [transform] function applied to elements of the given sequence.
|
||||||
|
* If any of two pairs would have the same key the last one gets added to the map.
|
||||||
|
*/
|
||||||
|
@kotlin.jvm.JvmName("toMapOfPairs")
|
||||||
|
public inline fun <T, K, V> Sequence<T>.toMap(transform: (T) -> Pair<K, V>): Map<K, V> {
|
||||||
|
val result = LinkedHashMap<K, V>()
|
||||||
|
for (element in this) {
|
||||||
|
result += transform(element)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a [Map] containing the elements from the given sequence indexed by the key
|
* Returns a [Map] containing the elements from the given sequence indexed by the key
|
||||||
* returned from [selector] function applied to each element.
|
* returned from [selector] function applied to each element.
|
||||||
|
|||||||
@@ -842,6 +842,20 @@ public inline fun <K, V> String.toMap(selector: (Char) -> K, transform: (Char) -
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a [Map] containing key-value pairs provided by [transform] function applied to characters of the given char sequence.
|
||||||
|
* If any of two pairs would have the same key the last one gets added to the map.
|
||||||
|
*/
|
||||||
|
@kotlin.jvm.JvmName("toMapOfPairs")
|
||||||
|
public inline fun <K, V> CharSequence.toMap(transform: (Char) -> Pair<K, V>): Map<K, V> {
|
||||||
|
val capacity = (length/.75f) + 1
|
||||||
|
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
|
||||||
|
for (element in this) {
|
||||||
|
result += transform(element)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a [Map] containing the characters from the given char sequence indexed by the key
|
* Returns a [Map] containing the characters from the given char sequence indexed by the key
|
||||||
* returned from [selector] function applied to each character.
|
* returned from [selector] function applied to each character.
|
||||||
|
|||||||
@@ -184,9 +184,17 @@ class MapTest {
|
|||||||
@test fun createWithSelectorForKeyAndValue() {
|
@test fun createWithSelectorForKeyAndValue() {
|
||||||
val map = listOf("a", "bb", "ccc").toMap({ it.length }, { it.toUpperCase() })
|
val map = listOf("a", "bb", "ccc").toMap({ it.length }, { it.toUpperCase() })
|
||||||
assertEquals(3, map.size)
|
assertEquals(3, map.size)
|
||||||
assertEquals("A", map.get(1))
|
assertEquals("A", map[1])
|
||||||
assertEquals("BB", map.get(2))
|
assertEquals("BB", map[2])
|
||||||
assertEquals("CCC", map.get(3))
|
assertEquals("CCC", map[3])
|
||||||
|
}
|
||||||
|
|
||||||
|
@test fun createWithPairSelector() {
|
||||||
|
val map = listOf("a", "bb", "ccc").toMap { it.length to it.toUpperCase() }
|
||||||
|
assertEquals(3, map.size)
|
||||||
|
assertEquals("A", map[1])
|
||||||
|
assertEquals("BB", map[2])
|
||||||
|
assertEquals("CCC", map[3])
|
||||||
}
|
}
|
||||||
|
|
||||||
@test fun createUsingTo() {
|
@test fun createUsingTo() {
|
||||||
|
|||||||
@@ -127,6 +127,60 @@ fun snapshots(): List<GenericFunction> {
|
|||||||
deprecate(Deprecation("Use toMapBy instead.", replaceWith = "toMapBy(selector)", level = DeprecationLevel.HIDDEN))
|
deprecate(Deprecation("Use toMapBy instead.", replaceWith = "toMapBy(selector)", level = DeprecationLevel.HIDDEN))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
templates add f("toMap(transform: (T) -> Pair<K, V>)") {
|
||||||
|
inline(true)
|
||||||
|
include(CharSequences)
|
||||||
|
typeParam("K")
|
||||||
|
typeParam("V")
|
||||||
|
returns("Map<K, V>")
|
||||||
|
annotations("""@kotlin.jvm.JvmName("toMapOfPairs")""")
|
||||||
|
doc { f ->
|
||||||
|
"""
|
||||||
|
Returns a [Map] containing key-value pairs provided by [transform] function applied to ${f.element}s of the given ${f.collection}.
|
||||||
|
If any of two pairs would have the same key the last one gets added to the map.
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
body {
|
||||||
|
"""
|
||||||
|
val capacity = (collectionSizeOrDefault(10)/.75f) + 1
|
||||||
|
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
|
||||||
|
for (element in this) {
|
||||||
|
result += transform(element)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
body(Sequences) {
|
||||||
|
"""
|
||||||
|
val result = LinkedHashMap<K, V>()
|
||||||
|
for (element in this) {
|
||||||
|
result += transform(element)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
body(CharSequences) {
|
||||||
|
"""
|
||||||
|
val capacity = (length/.75f) + 1
|
||||||
|
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
|
||||||
|
for (element in this) {
|
||||||
|
result += transform(element)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
body(ArraysOfObjects, ArraysOfPrimitives) {
|
||||||
|
"""
|
||||||
|
val capacity = (size/.75f) + 1
|
||||||
|
val result = LinkedHashMap<K, V>(Math.max(capacity.toInt(), 16))
|
||||||
|
for (element in this) {
|
||||||
|
result += transform(element)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
templates add f("toMapBy(selector: (T) -> K)") {
|
templates add f("toMapBy(selector: (T) -> K)") {
|
||||||
inline(true)
|
inline(true)
|
||||||
typeParam("K")
|
typeParam("K")
|
||||||
|
|||||||
Reference in New Issue
Block a user