Provide mutableSetOf and mutableMapOf

#KT-9663 Fixed
This commit is contained in:
Ilya Gorbunov
2016-01-16 05:49:57 +03:00
parent fe8ba4d356
commit aafd79078f
4 changed files with 17 additions and 1 deletions
@@ -46,6 +46,14 @@ public fun <K, V> mapOf(): Map<K, V> = emptyMap()
@JvmVersion
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
* 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). */
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. */
public fun <T> hashSetOf(vararg elements: T): HashSet<T> = elements.toCollection(HashSet(mapCapacity(elements.size)))