9

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?

Community
  • 1
  • 1
Philipp Claßen
  • 32,622
  • 19
  • 125
  • 194

2 Answers2

9

Known Java 8 interoperability issues are tracked as subtasks of this issue

Mibac
  • 7,250
  • 4
  • 30
  • 52
Philipp Claßen
  • 32,622
  • 19
  • 125
  • 194
3

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

Mibac
  • 7,250
  • 4
  • 30
  • 52
user2235698
  • 5,882
  • 1
  • 15
  • 21
  • Yes, I just noticed that Map is not java.util.Map. However, if I change my example to java.util.Map, it still fails to compile. I posted it as a separate question: http://stackoverflow.com/q/34255329/783510 – Philipp Claßen Dec 13 '15 at 19:31