It is finally possible to use collection literals in Kotlin! That means you can create a list, a set, or a custom collection with box brackets, just like in Python or JavaScript.
val numbers = [1, 2, 3, 4, 5] // This created a list
println(numbers) // [1, 2, 3, 4, 5]
val names: Set<String> = ["Alice", "Bob", "Charlie"] // This created a set
println(names) // [Alice, Bob, Charlie]
That was the most demanded feature in Kotlin. Collection literals won feature survey in first Kotlin features survey, and took second place in the second one. Let's dive into this feature.
Current status
As of April 2026, this feature is not for serious use yet. It should be available since Kotlin 2.4. Currently, the latest officially released version of Kotlin is 2.3.21. To use collection literals, I needed to use the unofficial "2.4.0-Beta2" version.
This feature is experimental in 2.4, so it needs the "-Xcollection-literals" compiler argument to be enabled.
freeCompilerArgs.add("-Xcollection-literals")
It is also not supported by the most recently released EAP version of IntelliJ IDEA (IntelliJ IDEA 2026.1.1). This means if you use it, Gradle will be able to build your project, but Kotlin will not recognize this feature and will show errors.
Collection literals should be supported in IntelliJ IDEA 2026.2, which should be released in EAS around 5th of May 2026, so pretty soon. For now, those small inconveniences should not stop us from exploring this long-waited feature.
Simple use examples
The idea is straightforward: We can use box brackets to create collections. By default, it creates a list, but explicit type can be used to create different kinds of collections.
val numbers = [1, 2, 3, 4, 5] // This created a list
println(numbers) // [1, 2, 3, 4, 5]
val names: Set<String> = ["Alice", "Bob", "Charlie"] // This created a set
println(names) // [Alice, Bob, Charlie]
In practice, we often use collections as arguments to functions. In such cases, collection literals can make the code more concise and readable for all collection types.
fun consumeList(list: List<Int>) {}
fun consumeSet(list: Set<Int>) {}
consumeList([1, 2, 3, 4, 5])
consumeSet(["Alice", "Bob", "Charlie"])
Under the hood, box bracket is just an operator corresponding to the of function from a collection companion object. So, for instance, the first example is just a syntactic sugar for List.of(1, 2, 3, 4, 5), and the second one is a sugar for Set.of("Alice", "Bob", "Charlie").
val list = [1, 2]
// is equivalent to:
val list = List.of(1, 2)
val set: Set<String> = ["A", "B"]
// is equivalent to:
val set: Set<String> = Set.of("A", "B")
val list2: MyCustomList<Int> = [1, 2]
// is equivalent to:
val list2: MyCustomList<Int> = MyCustomList.of(1, 2)
class MyCustomList<T> {
companion object { operator fun <T> of(vararg elements: T): MyCustomList<T> = TODO() }
}
Custom collections can also provide of operator in their companion objects to support collection literals.
fun main() {
val l: MyCollection = [1,2,3]
println(l) // MyCollection(elements=[1,2,3])
}
data class MyCollection(private val elements: List<Int>) {
companion object {
operator fun of(vararg elements: Int): MyCollection = MyCollection(elements.toList())
}
}
Ending
This is all you need to know about this feature. It is simple syntactic sugar for our code. In the future, support for Map collection literals might be supported as well. This, and all details of this feature implementation can be found in this KEEP. Here you can see them used in the KotlinConf application.
Marcin Moskala is a highly experienced developer and Kotlin instructor as the founder of Kt. Academy, an official JetBrains partner specializing in Kotlin training, Google Developers Expert, known for his significant contributions to the Kotlin community. Moskala is the author of several widely recognized books, including "Effective Kotlin," "Kotlin Coroutines," "Functional Kotlin," "Advanced Kotlin," "Kotlin Essentials," and "Android Development with Kotlin."
Beyond his literary achievements, Moskala is the author of the largest Medium publication dedicated to Kotlin. As a respected speaker, he has been invited to share his insights at numerous programming conferences, including events such as Droidcon and the prestigious Kotlin Conf, the premier conference dedicated to the Kotlin programming language.