# Create collections ## Construct from elements The most common way to create a collection is with the standard library functions [listOf<T>()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/list-of.html), [setOf<T>()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/set-of.html), [mutableListOf<T>()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/mutable-list-of.html), [mutableSetOf<T>()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/mutable-set-of.html). If you provide a comma-separated list of collection elements as arguments, the compiler detects the element type automatically. When creating empty collections, specify the type explicitly. ```KOTLIN val numbersSet = setOf("one", "two", "three", "four") val emptySet = mutableSetOf() ``` The same is available for maps with the functions [mapOf()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/map-of.html) and [mutableMapOf()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/mutable-map-of.html). The map's keys and values are passed as `Pair` objects (usually created with `to` infix function). ```KOTLIN val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key4" to 1) ``` Note that the `to` notation creates a short-living `Pair` object, so it's recommended that you use it only if performance isn't critical. To avoid excessive memory usage, use alternative ways. For example, you can create a mutable map and populate it using the write operations. The [apply()](scope-functions.html#apply) function can help to keep the initialization fluent here. ```KOTLIN val numbersMap = mutableMapOf().apply { this["one"] = "1"; this["two"] = "2" } ``` ## Create with collection builder functions Another way of creating a collection is to call a builder function – [buildList()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/build-list.html), [buildSet()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/build-set.html), or [buildMap()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/build-map.html). They create a new, mutable collection of the corresponding type, populate it using [write operations](collection-write.html), and return a read-only collection with the same elements: ```KOTLIN val map = buildMap { // this is MutableMap, types of key and value are inferred from the `put()` calls below put("a", 1) put("b", 0) put("c", 4) } println(map) // {a=1, b=0, c=4} ``` ## Empty collections There are also functions for creating collections without any elements: [emptyList()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/empty-list.html), [emptySet()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/empty-set.html), and [emptyMap()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/empty-map.html). When creating empty collections, you should specify the type of elements that the collection will hold. ```KOTLIN val empty = emptyList() ``` ## Initializer functions for lists For lists, there is a constructor-like function that takes the list size and the initializer function that defines the element value based on its index. ```KOTLIN fun main() { //sampleStart val doubled = List(3, { it * 2 }) // or MutableList if you want to change its content later println(doubled) //sampleEnd } ``` ## Concrete type constructors To create a concrete type collection, such as an `ArrayList` or `LinkedList`, you can use the available constructors for these types. Similar constructors are available for implementations of `Set` and `Map`. ```KOTLIN val linkedList = LinkedList(listOf("one", "two", "three")) val presizedSet = HashSet(32) ``` ## Copy To create a collection with the same elements as an existing collection, you can use copying functions. Collection copying functions from the standard library create shallow copy collections with references to the same elements. Thus, a change made to a collection element reflects in all its copies. Collection copying functions, such as [toList()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/to-list.html), [toMutableList()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/to-mutable-list.html), [toSet()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/to-set.html) and others, create a snapshot of a collection at a specific moment. Their result is a new collection of the same elements. If you add or remove elements from the original collection, this won't affect the copies. Copies may be changed independently of the source as well. ```KOTLIN class Person(var name: String) fun main() { //sampleStart val alice = Person("Alice") val sourceList = mutableListOf(alice, Person("Bob")) val copyList = sourceList.toList() sourceList.add(Person("Charles")) alice.name = "Alicia" println("First item's name is: ${sourceList[0].name} in source and ${copyList[0].name} in copy") println("List size is: ${sourceList.size} in source and ${copyList.size} in copy") //sampleEnd } ``` These functions can also be used for converting collections to other types, for example, build a set from a list or vice versa. ```KOTLIN fun main() { //sampleStart val sourceList = mutableListOf(1, 2, 3) val copySet = sourceList.toMutableSet() copySet.add(3) copySet.add(4) println(copySet) //sampleEnd } ``` Alternatively, you can create new references to the same collection instance. New references are created when you initialize a collection variable with an existing collection. So, when the collection instance is altered through a reference, the changes are reflected in all its references. ```KOTLIN fun main() { //sampleStart val sourceList = mutableListOf(1, 2, 3) val referenceList = sourceList referenceList.add(4) println("Source size: ${sourceList.size}") //sampleEnd } ``` Collection initialization can be used for restricting mutability. For example, if you create a `List` reference to a `MutableList`, the compiler will produce errors if you try to modify the collection through this reference. ```KOTLIN fun main() { //sampleStart val sourceList = mutableListOf(1, 2, 3) val referenceList: List = sourceList //referenceList.add(4) //compilation error sourceList.add(4) println(referenceList) // shows the current state of sourceList //sampleEnd } ``` ## Invoke functions on other collections Collections can be created as a result of various operations on other collections. For example, [filtering](collection-filtering.html) a list creates a new list of elements that match the filter: ```KOTLIN fun main() { //sampleStart val numbers = listOf("one", "two", "three", "four") val longerThan3 = numbers.filter { it.length > 3 } println(longerThan3) //sampleEnd } ``` [Mapping](collection-transformations.html#map) produces a list from a transformation's results: ```KOTLIN fun main() { //sampleStart val numbers = setOf(1, 2, 3) println(numbers.map { it * 3 }) println(numbers.mapIndexed { idx, value -> value * idx }) //sampleEnd } ``` [Association](collection-transformations.html#associate) produces maps: ```KOTLIN fun main() { //sampleStart val numbers = listOf("one", "two", "three", "four") println(numbers.associateWith { it.length }) //sampleEnd } ``` For more information about operations on collections in Kotlin, see [Collection operations overview](collection-operations.html).