Kotlin functions to create Lists, Maps, and Sets

With Kotlin you can create lists, maps, and sets with standard functions that are automatically in scope. Here are those functions.

Kotlin Arrays

I need to dig into this more but I know there are these Kotlin array-creation functions:

  • arrayOf() — creates kotlin.Array
  • intArrayOf()kotlin.IntArray
  • emptyArrayOf<T>()


val x = arrayOf(1,2,3)     // Kotlin.Array
val x = IntArrayOf(1,2,3)  // Kotlin.IntArray
val x = emptyArray<Int>()

Other Kotlin arrays that you can create are shown in the kotlin.Library.kt source code:

fun <reified @PureReifiable T> arrayOfNulls(size: Int): Array<T?>
fun doubleArrayOf(vararg elements: Double): DoubleArray
fun floatArrayOf(vararg elements: Float): FloatArray
fun longArrayOf(vararg elements: Long): LongArray
fun intArrayOf(vararg elements: Int): IntArray
fun charArrayOf(vararg elements: Char): CharArray
fun shortArrayOf(vararg elements: Short): ShortArray
fun byteArrayOf(vararg elements: Byte): ByteArray
fun booleanArrayOf(vararg elements: Boolean): BooleanArray

Notice not only the function names, but their return types as well. It’s also important to note that IntArray is not an Array, which you can get a hint of when I explicitly show the return types for some of these functions:

val a: Array<Int> = emptyArray<Int>()
val b: Array<Int> = arrayOf(1,2,3)
val c: IntArray = intArrayOf(1,2,3)

If you look at the IntArray source code you’ll see that it doesn’t extend Array.

Kotlin List functions

Use these functions to create lists in Kotlin:

Function Type
arrayListOf ArrayList<T>
emptyList List<T>
listOf List<T>
listOfNotNull List<T>
mutableListOf MutableList<T>

List examples:

val list = listOf(1, 2, 3)
val list = arrayListOf(1, 2, 3)
val list = mutableListOf("a", "b", "c")

// empty lists
val list = listOf<Int>()
val list = arrayListOf<Double>()
val list = mutableListOf<String>()
val list: List<Int> = emptyList()

// nullability
val list = listOf("a", null)                  // [a, null]
val list = arrayListOf("a", null)             // [a, null]
val list = mutableListOf("a", null)           // [a, null]
val list = listOfNotNull<String>("a", null)   // [a]
val list = listOfNotNull("a", null)           // [a]

Per the Kotlin docs, the difference between listOf and listOfNotNull:

  • listOf: Returns an immutable list containing only the specified object element.
  • listOfNotNull: Returns a new read-only list either of single given element, if it is not null, or empty list if the element is null.

I just noticed that there’s also this function:

  • intArrayOf()

At the moment I don’t know why that function exists but there aren’t similarly named functions like stringArrayOf or intListOf. (I do know from experimenting with how to show Kotlin data types in the REPL that intArrayOf returns an IntArray while arrayOf returns an Array.)

Kotlin Map functions

Use these functions to create Maps in Kotlin:

Function Type
mapOf Map<K,V>
hashMapOf HashMap<K, V>
linkedMapOf LinkedHashMap<K, V>
sortedMapOf SortedMap<K, V>
mutableMapOf MutableMap<K, V>

Kotlin also has linkedStringMapOf and stringMapOf functions for JavaScript.

Map examples:

val map = mapOf("b" to 2, "a" to 1)          // {b=2, a=1}
val map = hashMapOf("b" to 2, "a" to 1)      // {a=1, b=2}
val map = linkedMapOf("b" to 2, "a" to 1)    // {b=2, a=1}
val map = sortedMapOf("b" to 2, "a" to 1)    // {a=1, b=2}
val map = mutableMapOf("b" to 2, "a" to 1)   // {b=2, a=1}

Kotlin Set functions

Use these functions to create Sets in Kotlin:

Function Type
setOf Set<T>
hashSetOf HashSet<T>
linkedSetOf LinkedHashSet<T>
sortedSetOf TreeSet<T>
mutableSetOf MutableSet<T>

Kotlin also has linkedStringSetOf and stringSetOf functions for JavaScript.

Set examples:

val set = setOf(3, 5, 1)          // [3, 5, 1]
val set = hashSetOf(3, 5, 1)      // [5, 1, 3]
val set = linkedSetOf(3, 5, 1)    // [3, 5, 1]
val set = sortedSetOf(3, 5, 1)    // [1, 3, 5]
val set = mutableSetOf(3, 5, 1)   // [3, 5, 1]

Summary: Kotlin List, Map, and Set creation functions

I hope this list of functions that can be used to create Kotlin Lists, Maps, and Sets is helpful. For more information, see the kotlin.collections docs.