O'Reilly logo

Programming Kotlin by Stefan Bocutiu, Stephen Samuel

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Lists

Lists are ordered collections. With a list, you can insert an element at a very specific location, as well as retrieve elements by the position in the collection. Kotlin provides a couple of pre-built methods for constructing immutable and mutable lists. Remember, immutability is achieved via interface. Here is how you would create lists in idiomatic Kotlin:

 val intList: List<Int> = listOf println("Int list[${intList.javaClass.canonicalName}]:${intList.joinToString(", ")}") val emptyList: List<String> = emptyList<String>() println("Empty list[${emptyList.javaClass.canonicalName}]:${emptyList.joinToStrin g(",")}") val nonNulls: List<String> = listOfNotNull<String>(null, "a", "b", "c") println("Non-Null string lists[${nonNulls.javaClass.canonicalName}]:${nonNulls.joinToString ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required