Provide mutableSetOf and mutableMapOf
#KT-9663 Fixed
This commit is contained in:
@@ -46,6 +46,14 @@ public fun <K, V> mapOf(): Map<K, V> = emptyMap()
|
|||||||
@JvmVersion
|
@JvmVersion
|
||||||
public fun <K, V> mapOf(pair: Pair<K, V>): Map<K, V> = Collections.singletonMap(pair.first, pair.second)
|
public fun <K, V> mapOf(pair: Pair<K, V>): Map<K, V> = Collections.singletonMap(pair.first, pair.second)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a new [MutableMap] with the specified contents, given as a list of pairs
|
||||||
|
* where the first component is the key and the second is the value.
|
||||||
|
* This map preserves insertion order so iterating through the map's entries will be in the same order.
|
||||||
|
*/
|
||||||
|
public fun <K, V> mutableMapOf(vararg pairs: Pair<K, V>): MutableMap<K, V>
|
||||||
|
= LinkedHashMap<K, V>(mapCapacity(pairs.size)).apply { putAll(pairs) }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a new [HashMap] with the specified contents, given as a list of pairs
|
* Returns a new [HashMap] with the specified contents, given as a list of pairs
|
||||||
* where the first component is the key and the second is the value.
|
* where the first component is the key and the second is the value.
|
||||||
|
|||||||
@@ -31,6 +31,8 @@ public fun <T> setOf(vararg elements: T): Set<T> = if (elements.size > 0) elemen
|
|||||||
/** Returns an empty read-only set. The returned set is serializable (JVM). */
|
/** Returns an empty read-only set. The returned set is serializable (JVM). */
|
||||||
public fun <T> setOf(): Set<T> = emptySet()
|
public fun <T> setOf(): Set<T> = emptySet()
|
||||||
|
|
||||||
|
/** Returns a new [MutableSet] with the given elements. */
|
||||||
|
public fun <T> mutableSetOf(vararg elements: T): MutableSet<T> = elements.toCollection(LinkedHashSet(mapCapacity(elements.size)))
|
||||||
|
|
||||||
/** Returns a new [HashSet] with the given elements. */
|
/** Returns a new [HashSet] with the given elements. */
|
||||||
public fun <T> hashSetOf(vararg elements: T): HashSet<T> = elements.toCollection(HashSet(mapCapacity(elements.size)))
|
public fun <T> hashSetOf(vararg elements: T): HashSet<T> = elements.toCollection(HashSet(mapCapacity(elements.size)))
|
||||||
|
|||||||
@@ -209,6 +209,12 @@ class MapTest {
|
|||||||
assertEquals(2, map["b"])
|
assertEquals(2, map["b"])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test fun createMutableMap() {
|
||||||
|
val map = mutableMapOf("b" to 1, "c" to 2)
|
||||||
|
map.put("a", 3)
|
||||||
|
assertEquals(listOf("b" to 1, "c" to 2, "a" to 3), map.toList())
|
||||||
|
}
|
||||||
|
|
||||||
@test fun createLinkedMap() {
|
@test fun createLinkedMap() {
|
||||||
val map = linkedMapOf(Pair("c", 3), Pair("b", 2), Pair("a", 1))
|
val map = linkedMapOf(Pair("c", 3), Pair("b", 2), Pair("a", 1))
|
||||||
assertEquals(1, map["a"])
|
assertEquals(1, map["a"])
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ class SetOperationsTest {
|
|||||||
assertEquals(setOf("a", "foo", "beer", "cheese", "bar"), set)
|
assertEquals(setOf("a", "foo", "beer", "cheese", "bar"), set)
|
||||||
assertTrue(set !== setOriginal)
|
assertTrue(set !== setOriginal)
|
||||||
|
|
||||||
val mset = hashSetOf("a")
|
val mset = mutableSetOf("a")
|
||||||
mset += "foo"
|
mset += "foo"
|
||||||
mset += listOf("beer", "a")
|
mset += listOf("beer", "a")
|
||||||
mset += arrayOf("cheese", "beer")
|
mset += arrayOf("cheese", "beer")
|
||||||
|
|||||||
Reference in New Issue
Block a user