Is there an up-to-date overview of Java 8 features, which are not yet supported in Kotlin?
For example, calling a default method like Map#putIfAbsent fails to compile (unsupported reference error):
import java.util.*
fun main(args: Array<String>) {
val x : Map<Int, Int> = HashMap<Int, Int>()
x.putIfAbsent(1, 2)
}
If the default method is overridden, it works:
import java.util.*
fun main(args: Array<String>) {
val x : HashMap<Int, Int> = HashMap<Int, Int>()
x.putIfAbsent(1, 2)
}
That is what I found out by experiments, but for deciding whether migrating from a Java 8 code basis to Kotlin is already possible, a systematic overview would be valuable.
Update: The code in my example was created by the "Java to Kotlin" converter. As user2235698 pointed out, Map<Int, Int>
is a immutable Kotlin map. Still, the example fails to compile when I change it to a java.util.Map
map. My claim that it has to do something with default methods, however, is misleading.
As it is beyond the scope of this question, I opened a follow-up question, here: Does java.util.HashMap not implement java.util.Map in Kotlin?
Known Java 8 interoperability issues are tracked as subtasks of this issue
Map
is immutable and HashMap
is mutable in Kotlin, that's why you can't put key-value pair in the first case.
More details
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With