Collections interop behavior helps Kotlin work smoothly with Java collections. It makes using lists, sets, and maps from Java easy and safe in Kotlin.
Collections interop behavior in Kotlin
val kotlinList: List<String> = javaList val javaList: java.util.List<String> = kotlinList
Kotlin collections are interfaces that can wrap Java collections without copying.
You can use Kotlin collection functions on Java collections directly.
val javaList: java.util.List<String> = java.util.ArrayList() javaList.add("apple") val kotlinList: List<String> = javaList println(kotlinList[0])
val kotlinMutableList: MutableList<String> = mutableListOf("cat", "dog") val javaList: java.util.List<String> = kotlinMutableList println(javaList[1])
val javaMap: java.util.Map<String, Int> = java.util.HashMap() javaMap["one"] = 1 val kotlinMap: Map<String, Int> = javaMap println(kotlinMap["one"])
This program shows how Kotlin and Java collections can be used interchangeably without copying. Changes in Kotlin mutable list reflect in Java list.
fun main() { val javaList: java.util.List<String> = java.util.ArrayList() javaList.add("Kotlin") javaList.add("Java") // Use Java list as Kotlin list val kotlinList: List<String> = javaList println("First element from Kotlin list: ${kotlinList[0]}") // Use Kotlin mutable list as Java list val kotlinMutableList: MutableList<String> = mutableListOf("Apple", "Banana") val javaListFromKotlin: java.util.List<String> = kotlinMutableList println("Second element from Java list: ${javaListFromKotlin[1]}") // Modify Kotlin mutable list and see change in Java list kotlinMutableList.add("Cherry") println("Java list size after Kotlin list modification: ${javaListFromKotlin.size}") }
Kotlin collections wrap Java collections, so no extra memory is used.
Mutable collections in Kotlin correspond to Java collections that can be modified.
Be careful when modifying collections shared between Kotlin and Java to avoid unexpected changes.
Kotlin collections can wrap Java collections directly without copying.
This makes Kotlin and Java collections work smoothly together.
Mutable collections in Kotlin correspond to modifiable Java collections.
Practice
List inside Kotlin code, what happens to the collection?Solution
Step 1: Understand Kotlin-Java collection interop
Kotlin collections can wrap Java collections directly, meaning no new copy is made.Step 2: Identify behavior for Java List in Kotlin
Using a JavaListin Kotlin results in a wrapper around the original Java list, preserving changes.Final Answer:
Kotlin wraps the Java list directly without copying it. -> Option AQuick Check:
Kotlin wraps Java collections directly = D [OK]
- Thinking Kotlin copies Java collections
- Assuming Java collections convert to Kotlin sets
- Believing Java collections cause errors in Kotlin
ArrayList without copying?Solution
Step 1: Review Kotlin-Java mutable list interop
Kotlin mutable lists can be created by casting Java mutable lists directly without copying.Step 2: Analyze each option
val javaList = ArrayList<String>(); val kotlinList = javaList as MutableList<String>usesas MutableList<String>to wrap the JavaArrayListdirectly.toList()andtoMutableList()create copies, and there is nocopy()method.Final Answer:
val javaList = ArrayList<String>(); val kotlinList = javaList as MutableList<String> -> Option AQuick Check:
Cast Java list to MutableList for no copy = C [OK]
- Using toList() or toMutableList() which copies
- Trying to call non-existent copy() method
- Not casting and expecting automatic conversion
val javaList = java.util.ArrayList<String>()
javaList.add("Kotlin")
val kotlinList: List<String> = javaList
println(kotlinList[0])
javaList.add("Java")
println(kotlinList.size)Solution
Step 1: Understand Kotlin wrapping Java list
The KotlinListwraps the JavaArrayListdirectly, so changes to javaList reflect in kotlinList.Step 2: Trace the code output
First print prints "Kotlin" (element at index 0). Then adding "Java" to javaList increases size to 2, so second print outputs 2.Final Answer:
Kotlin 2 -> Option CQuick Check:
Wrapped list reflects changes = A [OK]
- Assuming kotlinList is a copy and size stays 1
- Expecting compilation error due to Java interop
- Confusing index access with size
val javaList = java.util.ArrayList<String>()
val kotlinList: MutableList<String> = javaList
kotlinList.add("Hello")
println(javaList.size)What is the problem with this code?
Solution
Step 1: Analyze Kotlin-Java mutable list assignment
Assigning a JavaArrayListto a KotlinMutableListvariable works because Kotlin treats Java mutable collections as mutable Kotlin collections.Step 2: Trace mutation effect
Adding "Hello" to kotlinList modifies the underlying javaList, so javaList.size becomes 1.Final Answer:
It compiles and javaList.size becomes 1. -> Option BQuick Check:
Mutable Kotlin list wraps Java list, changes reflect = A [OK]
- Expecting runtime ClassCastException
- Thinking javaList.size stays 0 after add
- Assuming compilation error on assignment
HashMap<String, Integer> and want to use it in Kotlin as a mutable map without copying. Which Kotlin code correctly achieves this and allows modifying the map?Solution
Step 1: Understand Kotlin-Java map interop
Kotlin mutable maps can directly wrap Java mutable maps likeHashMapwithout copying.Step 2: Evaluate options for mutability and copying
The direct assignmentval kotlinMap: MutableMap<String, Int> = javaMapwraps the JavaHashMapwithout copying and preserves mutability. In contrast,toMap()and casting toMapprovide immutable access, whiletoMutableMap()creates a new mutable copy.Final Answer:
val javaMap = java.util.HashMap<String, Int>() val kotlinMap: MutableMap<String, Int> = javaMap -> Option DQuick Check:
Assign Java HashMap to MutableMap for no copy and mutability = B [OK]
- Using toMap() or toMutableMap() which copies
- Casting to Map loses mutability
- Assuming Kotlin creates a new map automatically
Ready to prove it?
Pass the test and this pattern moves unknown
