How to access methods such as listOf
, mapOf
and setOf
from Java.
I'm in a project that doesn't use Kotlin but we want to use the effectively read-only Kotlin collections. Why? Because even when using immutable lists in Java, they have mutator methods that will raise an error in runtime, and not during compilation.
Asked
Active
Viewed 61 times
1

caeus
- 3,084
- 1
- 22
- 36
-
1You can access those functions via `CollectionsKt`, `MapKt` and `SetsKt` from Java... regarding your Kotlin vs Java `List` it is a bit more complicated... if you look at the return value of those methods from the Java side, you will see, that you don't get the Kotlin types there, but already have the Java ones in place... – Roland Aug 08 '19 at 12:57
-
@gpunto. Definitely not a duplicate. I want to use the Kotlin collections library here. Not the equivalent in Java collections. – caeus Aug 08 '19 at 13:44
-
The Kotlin collections *are* the Java collections. The Kotlin compiler just does some magic on the Java types to split it into different interfaces. These Kotlin types *replace* the Java types and as such only exist in Kotlin. – RobCo Aug 08 '19 at 15:30
-
@RobCo, is that for real? Can you show me some source stating so? If that's the case, is there any library for read-only collections? – caeus Aug 08 '19 at 15:38