fun main(args : Array<String>) {
println(arrayList(“a”, “b”).javaClass)
println(linkedList(“a”, “b”).javaClass)
println(hashMap(“a” to 123, “b” to 456).javaClass)
println(sortedMap(“b” to 456, “a” to 123).javaClass)
println(linkedMap(“a” to 123, “b” to 456).javaClass)
println(hashSet(“a”, “b”).javaClass)
println(sortedSet(“a”, “b”).javaClass)
}
> output class java.util.ArrayList class java.util.LinkedList class java.util.HashMap class java.util.TreeMap class java.util.LinkedHashMap class java.util.HashSet class java.util.TreeSet
jet.Collections are default collections in Kotlin. So you write
fun foo(list: List<Int>)
when you want a list (it means jet.List).
Or you write
fun foo(mutableList: MutableList<Int>)
when you want to be able to change this list.
As you could have noticed, jet.Collections are enhanced Java’s interfaces (to have easy compatibility).
Read-only and mutable collections are distinguished by different traits (“interfaces”)
(more in http://blog.jetbrains.com/kotlin/2012/09/kotlin-m3-is-out/, part about Collections).
What’s important, in bytecode both jet.List and jet.MutableList are java.util.List (this is the core of easy compatibility).
So in Java you will see standard Java interfaces, not Kotlin.
For now default implementations are Java’s, so you see “java.util.ArrayList” when you invoke ‘javaClass’.
I’ve read that blog link you gave, but I think the explanation is a bit short, and it still leave some questions for me. Let me see if I understand what you/blog is saying. So in Kotlin, we create a dynamic list by calling built-in “arrayList()” function. The default Kotlin is using JVM, so the result is the JDK’s java.util.ArrayList instance.
Now Kotlin has introduce some magic that an instance of java.util.ArrayList would automatically implements two Kotlin’s traits: jet.List and jet.MutableList. Kotlin code can get these two objects by simply casting such as following in variables ls3 and ls4:
``
val ls1 = arrayList(1,2,3)
val ls2 : java.util.ArrayList<Int> = arrayList(1,2,3)
val ls3 : MutableList<Int> = arrayList(1,2,3)
val ls4 : List<Int> = arrayList(1,2,3)
One would expect ls1 type is same as ls2. While all ls1, ls2, ls3 are all mutable, but ls4 is a read only variable.