Skip to content
← Snippets

Creating Collections with Repeating Elements in Kotlin

Learn how to create different collections such as Lists, MutableLists, or even Arrays with repeating elements in them

Oct 18, 2023 · 1 min read

Use:

ListTest.kt
val list = List(50) { index -> "Something" }
assertThat(list).hasSize(50)

Since index is unused, you could omit it:

val list = List(50) { "Something" }

Or, even using data class:

data class Something (
  val id: Long? = null
)
 
val somethings = List(3) { Something() }
Creating Collections with Repeating Elements in Kotlin | Viren Soni | Software Engineer