From 076a94f7f2804b9a5990f4c97b400b7635f63427 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Mon, 2 Jan 2017 07:16:53 +0300 Subject: [PATCH] Provide parameterless mutable collection factory functions. #KT-15630 --- .../src/kotlin/collections/Collections.kt | 10 ++++++++ .../stdlib/src/kotlin/collections/Maps.kt | 23 +++++++++++++++++++ .../stdlib/src/kotlin/collections/Sets.kt | 19 +++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/libraries/stdlib/src/kotlin/collections/Collections.kt b/libraries/stdlib/src/kotlin/collections/Collections.kt index 96cb57725e4..1fd4b18facd 100644 --- a/libraries/stdlib/src/kotlin/collections/Collections.kt +++ b/libraries/stdlib/src/kotlin/collections/Collections.kt @@ -90,6 +90,16 @@ public inline fun listOf(): List = emptyList() @JvmVersion public fun listOf(element: T): List = java.util.Collections.singletonList(element) +/** Returns an empty new [MutableList]. */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun mutableListOf(): MutableList = ArrayList() + +/** Returns an empty new [ArrayList]. */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun arrayListOf(): ArrayList = ArrayList() + /** Returns a new [MutableList] with the given elements. */ public fun mutableListOf(vararg elements: T): MutableList = if (elements.size == 0) ArrayList() else ArrayList(ArrayAsCollection(elements, isVarargs = true)) diff --git a/libraries/stdlib/src/kotlin/collections/Maps.kt b/libraries/stdlib/src/kotlin/collections/Maps.kt index e52e65f4fd0..7908b311214 100644 --- a/libraries/stdlib/src/kotlin/collections/Maps.kt +++ b/libraries/stdlib/src/kotlin/collections/Maps.kt @@ -56,6 +56,16 @@ public inline fun mapOf(): Map = emptyMap() @JvmVersion public fun mapOf(pair: Pair): Map = java.util.Collections.singletonMap(pair.first, pair.second) +/** + * Returns an empty new [MutableMap]. + * + * The returned map preserves the entry iteration order. + * @sample samples.collections.Maps.Instantiation.emptyMutableMap + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun mutableMapOf(): MutableMap = LinkedHashMap() + /** * 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. If multiple pairs have @@ -67,6 +77,13 @@ public fun mapOf(pair: Pair): Map = java.util.Collections.sin public fun mutableMapOf(vararg pairs: Pair): MutableMap = LinkedHashMap(mapCapacity(pairs.size)).apply { putAll(pairs) } +/** + * Returns an empty new [HashMap]. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun hashMapOf(): HashMap = HashMap() + /** * 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. @@ -76,6 +93,12 @@ public fun mutableMapOf(vararg pairs: Pair): MutableMap public fun hashMapOf(vararg pairs: Pair): HashMap = HashMap(mapCapacity(pairs.size)).apply { putAll(pairs) } +/** + * Returns an empty new [LinkedHashMap]. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun linkedMapOf(): LinkedHashMap = LinkedHashMap() /** * Returns a new [LinkedHashMap] with the specified contents, given as a list of pairs diff --git a/libraries/stdlib/src/kotlin/collections/Sets.kt b/libraries/stdlib/src/kotlin/collections/Sets.kt index aaf5f3f0ab7..2f8c2ae3ff6 100644 --- a/libraries/stdlib/src/kotlin/collections/Sets.kt +++ b/libraries/stdlib/src/kotlin/collections/Sets.kt @@ -35,15 +35,34 @@ public fun setOf(vararg elements: T): Set = if (elements.size > 0) elemen @kotlin.internal.InlineOnly public inline fun setOf(): Set = emptySet() +/** + * Returns an empty new [MutableSet]. + * + * The returned set preserves the element iteration order. + */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun mutableSetOf(): MutableSet = LinkedHashSet() + /** * Returns a new [MutableSet] with the given elements. * Elements of the set are iterated in the order they were specified. */ public fun mutableSetOf(vararg elements: T): MutableSet = elements.toCollection(LinkedHashSet(mapCapacity(elements.size))) +/** Returns an empty new [HashSet]. */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun hashSetOf(): HashSet = HashSet() + /** Returns a new [HashSet] with the given elements. */ public fun hashSetOf(vararg elements: T): HashSet = elements.toCollection(HashSet(mapCapacity(elements.size))) +/** Returns an empty new [LinkedHashSet]. */ +@SinceKotlin("1.1") +@kotlin.internal.InlineOnly +public inline fun linkedSetOf(): LinkedHashSet = LinkedHashSet() + /** * Returns a new [LinkedHashSet] with the given elements. * Elements of the set are iterated in the order they were specified.