# Kotlin.Collection vs. java.util.Collection

**URL:** <https://discuss.kotlinlang.org/t/kotlin-collection-vs-java-util-collection/256>\
**Category:** Uncategorized\
**Created:** [May 5, 2015, 1:05pm UTC](https://discuss.kotlinlang.org/t/kotlin-collection-vs-java-util-collection/256 "2015-05-05T13:05:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Oliver\_Plohmann](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/oliver_plohmann/32/2398_2.png) [@Oliver\_Plohmann](https://discuss.kotlinlang.org/u/Oliver_Plohmann)\
**Post date:** [May 5, 2015, 1:05pm UTC](https://discuss.kotlinlang.org/t/kotlin-collection-vs-java-util-collection/256/1 "2015-05-05T13:05:00Z")

</div>

Hello,

I just realised that this doesn’t compile (using Kotlin 0.11.1014):

> val coll: java.util.Collection\<String\> = java.util.ArrayList\<String\>()

I get this error:

Error:(14, 46) Kotlin: Type mismatch: inferred type is java.util.ArrayList\<kotlin.String\> but java.util.Collection\<kotlin.String\> was expected

It works when I do an explicit cast:

> val coll: java.util.Collection\<String\> = java.util.ArrayList\<String\>() as java.util.Collection\<String\>

It is not clear to me whether this should work or whether I'm missing something. At least, I would expect this to work. It's not intuitive to me why I need to add the cast to java.util.Collection\<String\>. If someone could illuminate me on this.

Thanks, Oliver

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [May 7, 2015, 5:22am UTC](https://discuss.kotlinlang.org/t/kotlin-collection-vs-java-util-collection/256/2 "2015-05-07T05:22:06Z")

</div>

The way it works curretly is that whenver we encounter a collection in Kotlin, we load a Kotlin version of this class (e.g. kotlin.Collection) instead of a Java version (java.util.\*). Using the type java.util.Collection leads to a warning from the Kotlin compiler, because Kotlin's type checker is designed to distinguish between read-only and mutable collections. Why do you need to use java.util.Collection in your Kotlin code at all?

---

<div class="post-metadata">

**Author:** ![Oliver\_Plohmann](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/oliver_plohmann/32/2398_2.png) [@Oliver\_Plohmann](https://discuss.kotlinlang.org/u/Oliver_Plohmann)\
**Post date:** [May 7, 2015, 7:27am UTC](https://discuss.kotlinlang.org/t/kotlin-collection-vs-java-util-collection/256/3 "2015-05-07T07:27:29Z")

</div>

Problem is that f.ex. this does not compile:

> val set = setOf(2, 5, 8) val result = set.filter { it \> 2 } result.add(8) // Error:(13, 12) Kotlin: Unresolved reference: add

So I was looking for an alternative solution. By the way, this does not compile with the same error although toSet() is called:

> val set = setOf(2, 5, 8) val result = set.filter { it \> 2 }**.toSet()** result.add(8) // Error:(13, 12) Kotlin: Unresolved reference: add

Funny thing is that this prints "class java.util.ArrayList":

> val set = setOf(2, 5, 8) val result = set.filter { it \> 2 } println(result.javaClass)

Yet you can't call add on an&nbsp;&nbsp;java.util.ArrayList ... Am using Kotlin 0.11.1079

---

<div class="post-metadata">

**Author:** ![geevee](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/geevee/32/1475_2.png) [@geevee](https://discuss.kotlinlang.org/u/geevee)\
**Post date:** [May 12, 2015, 4:16pm UTC](https://discuss.kotlinlang.org/t/kotlin-collection-vs-java-util-collection/256/4 "2015-05-12T16:16:31Z")

</div>

Kotlin distinguished between read-only collections (subtypes of koltin.Collection) and mutable collections (subtypes of kotlin.MutableCollection). See [blog post about Kotlin M3](http://blog.jetbrains.com/kotlin/2012/09/kotlin-m3-is-out/) for extra details (including built-in collection types).

setOf(), filter() and toSet() in context of your sample return read-only collections, so you can’t call add on them. If you need mutable collection, use toHashSet() or toArrayList() instead:

> val set = setOf(2, 5, 8) val result = set.filter { it \> 2 }.toHashSet() result.add(8)

---

<div class="post-metadata">

**Author:** ![Oliver\_Plohmann](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/oliver_plohmann/32/2398_2.png) [@Oliver\_Plohmann](https://discuss.kotlinlang.org/u/Oliver_Plohmann)\
**Post date:** [May 13, 2015, 1:32pm UTC](https://discuss.kotlinlang.org/t/kotlin-collection-vs-java-util-collection/256/5 "2015-05-13T13:32:56Z")

</div>

> If you need mutable collection, use toHashSet() or toArrayList() instead.

All right, I see. Once Kotlin turns 1.0 the news will spread and people will take Kotlin for a spin. Once they see that

> val coll: java.util.Collection\<String\> = java.util.ArrayList\<String\>()

does not compile it will be considered weird, though.

---

<div class="post-metadata">

**Author:** ![geevee](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/geevee/32/1475_2.png) [@geevee](https://discuss.kotlinlang.org/u/geevee)\
**Post date:** [May 14, 2015, 3:11pm UTC](https://discuss.kotlinlang.org/t/kotlin-collection-vs-java-util-collection/256/6 "2015-05-14T15:11:15Z")

</div>

It's not a big problem: they will get warning when using "java.util.Collection" anyway.

---

<div class="post-metadata">

**Author:** ![ngalarneau](https://avatars.discourse-cdn.com/v4/letter/n/6de8d8/32.png) [@ngalarneau](https://discuss.kotlinlang.org/u/ngalarneau)\
**Post date:** [May 15, 2015, 2:48pm UTC](https://discuss.kotlinlang.org/t/kotlin-collection-vs-java-util-collection/256/7 "2015-05-15T14:48:33Z")

</div>

Why will there be a warning?

I thought the goal of Kotlin was to be highly interoperable with Java.

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [May 15, 2015, 4:30pm UTC](https://discuss.kotlinlang.org/t/kotlin-collection-vs-java-util-collection/256/8 "2015-05-15T16:30:16Z")

</div>

Kotlin is highly interoperable with Java. But there's no point in using some Java types (Collection, Integer etc) in Kotlin programs directly, hence the warning.
