This post shows how to create and use Lists in Kotlin
. It will also briefly touch on mutable and immutable lists.
Requirements
IntelliJ
IDEA
Ultimate 2016.3- The
Community Edition
maybe enough, but we have not tried it.
- The
Kotlin
Version 1.1- Windows 10 Enterprise
Java vs. Kotlin Collections
Although Kotlin
is 100% interoperable with Java
, it does not simply provide wrapped-around-Java-APIs
APIs
. It has its own set of APIs
some classes that don’t “extend” from Java APIs
.
Yes, some other classes “extend” or are used directly via typealias
. For instance,
1 | @SinceKotlin("1.1") public typealias ArrayList<E> = java.util.ArrayList<E> |
The image below shows the Kotlin Collections API
.
Using Java Collections API (in Kotlin)
Below is an example that uses java.util.ArrayList
.
1 2 3 4 5 6 | fun main(args: Array<String>) { val myList:java.util.ArrayList<String> = java.util.ArrayList<String>() myList.add("A") myList.add("B") println(myList) } |
However, we cannot use the interface java.util.List
as the reference type.
It’s best to avoid something like this unless there’s a need for it. Only use classes from Kotlin
.
Using Kotlin Collections API
We generally have 2 types of lists – immutable and mutable. Immutable lists are lists whose contents we cannot change. We cannot add or remove elements. Mutable lists are the opposite. We can change their contents.
NOTE: Reference list types that don’t have methods to change the contents of the objects they are referring to are essentially immutable. So which reference types make objects immutable? The only way to know is to look into each class or interface to be used provided in Kotlin Collections API
.
Here are some codes that are similar to the above codes but use only Kotlin Collections API
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | fun main(args: Array<String>) { /** * This is equivalent to: * * val myList:kotlin.collections.List<String> = kotlin.collections.listOf() * */ val myList:List<String> = listOf() /** * The List interface does not have add*, and remove* methods. * Thus, the two lines below will cause compile-time errors. */ // myList.add("A") // myList.add("B") println(myList) } |
Mutable Lists
Below is an example that uses a mutable list. The interface kotlin.collections.MutableList
has the add(element: E)
method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | fun main(args: Array<String>) { /** * This is equivalent to: * * val myList:kotlin.collections.MutableList<String> = kotlin.collections.mutableListOf() * */ val myList:MutableList<String> = mutableListOf() /** * The MutableList interface has add*, and remove* methods. * Thus, the two lines below will work. */ myList.add("A") myList.add("B") println(myList) } |
Testing a Mutable List Example
Immutable List
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package exercise01 fun main(args: Array<String>) { /** * This is equivalent to: * * val myList:kotlin.collections.MutableList<String> = kotlin.collections.mutableListOf() * */ val myList:kotlin.collections.List<String> = kotlin.collections.listOf() /** * The List interface has no add*, and remove* methods. * Thus, the two lines below will not work. */ myList.add("A") // Compile-time error myList.add("B") // Compile-time error println(myList) } |
Testing an immutable List Example
The following codes demonstrate an immutable list using the appropriate factory method and reference type.
References
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/index.html