Provide extensions to copy maps.

#KT-9108
This commit is contained in:
Ilya Gorbunov
2016-05-13 17:42:28 +03:00
parent 0ce4bce4a5
commit efc8f2c88a
3 changed files with 55 additions and 10 deletions
@@ -438,6 +438,29 @@ public fun <K, V> Sequence<Pair<K, V>>.toMap(): Map<K, V> = toMap(LinkedHashMap<
public fun <K, V, M : MutableMap<in K, in V>> Sequence<Pair<K, V>>.toMap(destination: M): M
= destination.apply { putAll(this@toMap) }
/**
* Returns a new read-only map containing all key-value pairs from the original map.
*
* The returned map preserves the entry iteration order of the original map.
*/
public fun <K, V> Map<out K, V>.toMap(): Map<K, V> = when (size) {
0 -> emptyMap()
else -> toMutableMap()
}
/**
* Returns a new mutable map containing all key-value pairs from the original map.
*
* The returned map preserves the entry iteration order of the original map.
*/
public fun <K, V> Map<out K, V>.toMutableMap(): MutableMap<K, V> = LinkedHashMap(this)
/**
* Populates and returns the [destination] mutable map with key-value pairs from the given map.
*/
public fun <K, V, M : MutableMap<in K, in V>> Map<out K, V>.toMap(destination: M): M
= destination.apply { putAll(this@toMap) }
/**
* Creates a new read-only map by replacing or adding an entry to this map from a given key-value [pair].
*
@@ -532,6 +555,7 @@ internal fun <K, V> Map<K, V>.optimizeReadOnlyMap() = when (size) {
else -> this
}
// creates a singleton copy of map, if it there is specialization available in target platform
@kotlin.jvm.JvmVersion
internal fun <K, V> Map<K, V>.toSingletonMap(): Map<K, V>
= with (entries.iterator().next()) { Collections.singletonMap(key, value) }
+28 -10
View File
@@ -162,18 +162,36 @@ class MapTest {
assertEquals("Mells", m2["location2"])
}
@test fun createUsingPairs() {
val map = mapOf(Pair("a", 1), Pair("b", 2))
assertEquals(2, map.size)
assertEquals(1, map["a"])
assertEquals(2, map["b"])
@test fun createFrom() {
val pairs = arrayOf("a" to 1, "b" to 2)
val expected = mapOf(*pairs)
assertEquals(expected, pairs.toMap())
assertEquals(expected, pairs.asIterable().toMap())
assertEquals(expected, pairs.asSequence().toMap())
assertEquals(expected, expected.toMap())
val mutableMap = expected.toMutableMap()
assertEquals(expected, mutableMap)
mutableMap += "c" to 3
assertNotEquals(expected, mutableMap)
}
@test fun createFromIterable() {
val map = listOf(Pair("a", 1), Pair("b", 2)).toMap()
assertEquals(2, map.size)
assertEquals(1, map.get("a"))
assertEquals(2, map.get("b"))
@test fun populateTo() {
val pairs = arrayOf("a" to 1, "b" to 2)
val expected = mapOf(*pairs)
val linkedMap: LinkedHashMap<String, Int> = pairs.toMap(linkedMapOf())
assertEquals(expected, linkedMap)
val hashMap: HashMap<String, Int> = pairs.asIterable().toMap(hashMapOf())
assertEquals(expected, hashMap)
val mutableMap: MutableMap<String, Int> = pairs.asSequence().toMap(mutableMapOf())
assertEquals(expected, mutableMap)
val mutableMap2 = mutableMap.toMap(mutableMapOf())
assertEquals(expected, mutableMap2)
}
@test fun createWithSelector() {
@@ -1532,10 +1532,13 @@ public final class kotlin/collections/MapsKt {
public static final fun toList (Ljava/util/Map;)Ljava/util/List;
public static final fun toMap (Ljava/lang/Iterable;)Ljava/util/Map;
public static final fun toMap (Ljava/lang/Iterable;Ljava/util/Map;)Ljava/util/Map;
public static final fun toMap (Ljava/util/Map;)Ljava/util/Map;
public static final fun toMap (Ljava/util/Map;Ljava/util/Map;)Ljava/util/Map;
public static final fun toMap (Lkotlin/sequences/Sequence;)Ljava/util/Map;
public static final fun toMap (Lkotlin/sequences/Sequence;Ljava/util/Map;)Ljava/util/Map;
public static final fun toMap ([Lkotlin/Pair;)Ljava/util/Map;
public static final fun toMap ([Lkotlin/Pair;Ljava/util/Map;)Ljava/util/Map;
public static final fun toMutableMap (Ljava/util/Map;)Ljava/util/Map;
public static final fun toSortedMap (Ljava/util/Map;)Ljava/util/SortedMap;
public static final fun toSortedMap (Ljava/util/Map;Ljava/util/Comparator;)Ljava/util/SortedMap;
public static final fun withDefault (Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;