16

I have trouble understanding why a Java HashMap is incompatible with a Java Map in Kotlin:

val map : java.util.Map<Int, Int> = java.util.HashMap<Int, Int>()
                                    // ERROR: Type mismatch

Is this a bug or is it intentionally enforced to be an error in Kotlin?


Here is a second example with the Java to Kotlin compiler. Take a look at this Java example file:

public class Test {
    public static void main(String[] args) {
        java.util.Map<Integer, Integer> map = new java.util.HashMap<>();
        insertValue(map);
    }

    private static void insertValue(java.util.Map<Integer, Integer> map) {
        map.putIfAbsent(0, 1);
    }
}

Running "Convert Java to Kotlin" results in this file:

object Test {
    @JvmStatic fun main(args: Array<String>) {
        val map = java.util.HashMap<Int, Int>()
        insertValue(map)
    }

    private fun insertValue(map: Map<Int, Int>) {
        map.putIfAbsent(0, 1)  // ERROR! Unresolved reference
    }
}

If I try to modify it to reflect more the original file, where insertValue expected a Java java.util.Map, I get another error:

object Test {
    @JvmStatic fun main(args: Array<String>) {
        val map = java.util.HashMap<Int, Int>()
        insertValue(map)  // ERROR: Type mismatch
    }

    private fun insertValue(map: java.util.Map<Int, Int>) {
        map.putIfAbsent(0, 1)  // is now OK
    }
}
Philipp Claßen
  • 41,306
  • 31
  • 146
  • 239

3 Answers3

14

In Kotlin, a java.util.HashMap, at compile time, does not implement java.util.Map, but implements kotlin.collections.MutableMap which extends the read-only kotlin.collections.Map.

This is because of Kotlin's mapped types. See Collections in Kotlin M3 is Out! for more details.

MutableMap does not define any putIfAbsent, but it has an extension function getOrPut.

Ilya
  • 21,871
  • 8
  • 73
  • 92
mfulton26
  • 29,956
  • 6
  • 64
  • 88
  • 1
    Are you sure? I thought that `kotlin.MutableMap` does not exist at runtime? – voddan Dec 14 '15 at 08:45
  • Correct. That is why I wrote "at compile time" in my answer. Kotlin's [mapped types](https://kotlinlang.org/docs/reference/java-interop.html#mapped-types) are available at compile-time but then mapped to different types at runtime. More details on platform types [here](https://kotlinlang.org/docs/reference/java-interop.html#null-safety-and-platform-types) and [here](https://kotlinlang.org/docs/reference/java-interop.html#notation-for-platform-types). – mfulton26 Dec 14 '15 at 11:26
3

I created a bug report, primarily for the "Java to Kotlin" converter that generates invalid Kotlin code in my original example:

KT-10400 Java to Kotlin converter: converting Java Code where a java.util.HashMap is accessed through the java.util.Map interface fails

I also mentioned that java.util.HashMap does not implement java.util.Map but I leave it to the Kotlin developers to decide whether it is a bug or whether the compiler is right to reject it.

UPDATE: The bug is marked as solved (commit 5e231db6).

Philipp Claßen
  • 41,306
  • 31
  • 146
  • 239
0

I think, it is a bug. You can report it there https://youtrack.jetbrains.com/issues/KT

But still, putIfAbsent is a Java 8 feature, not yet supported in Kotlin (MutableMap interface missing this method).

Eugene Krivenja
  • 647
  • 8
  • 18
  • 1
    I suspect the down votes were because of lack of references backing up this answer. e.g. This answer may have been considered more helpful by some if a link to an issue or relevant documentation about [calling Java from Kotlin](https://kotlinlang.org/docs/reference/java-interop.html) were provided which states/shows that Java Collections are not treated specially in Kotlin. – mfulton26 Dec 14 '15 at 11:47
  • Personally I appreciate the answer but I wish it referenced a concrete issue or some documentation supporting that it might be a bug. – mfulton26 Dec 14 '15 at 12:13
  • 1
    no need to down vote, the only supporting fact if that's a bug would be the illogicality of this behavior – voddan Dec 14 '15 at 14:18