# Immutable collections - perhaps guava integration or optional extension module?

**URL:** <https://discuss.kotlinlang.org/t/immutable-collections-perhaps-guava-integration-or-optional-extension-module/421>\
**Category:** Uncategorized\
**Created:** [November 3, 2014, 8:43pm UTC](https://discuss.kotlinlang.org/t/immutable-collections-perhaps-guava-integration-or-optional-extension-module/421 "2014-11-03T20:43:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rbygrave](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/rbygrave/32/1433_2.png) [@rbygrave](https://discuss.kotlinlang.org/u/rbygrave)\
**Post date:** [November 3, 2014, 8:43pm UTC](https://discuss.kotlinlang.org/t/immutable-collections-perhaps-guava-integration-or-optional-extension-module/421/1 "2014-11-03T20:43:30Z")

</div>

Hi,

I’m wondering if there is some plans on integrating or to some extent replicating the google guava immutable collections.&nbsp;&nbsp;Perhaps an optional ‘Immutable collections’ module would be a nice for those who want functionality similar to Guava’s immutable collections (and returned the Koltin read only List, Set etc).

```
&nbsp;&nbsp;// using google guava collections - no extensions
&nbsp;&nbsp;val immutableSet = ImmutableSet.of("red","green")
&nbsp;&nbsp;immutableSet.add("blue") // compile still allows

```

Is there a recommended alternative to using java.util.Collections.unmodifiableSet() ?

```
&nbsp;&nbsp;// kotlin.MutableList
&nbsp;&nbsp;val mutList = arrayListOf("one")

&nbsp;&nbsp;// returns a java.util.List / kotlin.MutableList
&nbsp;&nbsp;val readOnlyList = Collections.unmodifiableList(mutList);
&nbsp;&nbsp;readOnlyList.add("two") // allowed by compiler

&nbsp;&nbsp;// but I can type it to a kotlin.List ...
&nbsp;&nbsp;val readOnlyList2 : kotlin.List&lt;String&gt; = Collections.unmodifiableList(mutList);
&nbsp;&nbsp;readOnlyList2.add("two") // does not compile

```

Cheers, Rob.

---

<div class="post-metadata">

**Author:** ![rbygrave](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/rbygrave/32/1433_2.png) [@rbygrave](https://discuss.kotlinlang.org/u/rbygrave)\
**Post date:** [November 4, 2014, 1:35am UTC](https://discuss.kotlinlang.org/t/immutable-collections-perhaps-guava-integration-or-optional-extension-module/421/2 "2014-11-04T01:35:49Z")

</div>

I've just had another conversation on this and the general thoughts were:

_\> integrating or to some extent replicating the google guava immutable collections_

Most cases are going to be satisfied by the existing functions listOf(), mapOf() etc.&nbsp;&nbsp;The need for guava like immutable builders or greater immutable protection than already provided by the read only Kotlin List, Set, Map etc is very likely unwarranted.

_\> Is there a recommended alternative to using java.util.Collections.unmodifiableSet() ?_

Just using the explicit read only Kotlin type is easy enough and does the job but there might be a better approach.  
On MutableSet/MutableList there is the copyToArray() method so perhaps a similar copy() or unmodifiable() method would be a decent approach?

Thanks, Rob.

---

<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:** [November 4, 2014, 4:49am UTC](https://discuss.kotlinlang.org/t/immutable-collections-perhaps-guava-integration-or-optional-extension-module/421/3 "2014-11-04T04:49:57Z")

</div>

toList()/toSet()/toMap() seem to be what you are looking for with the "copy" method

---

<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:** [November 4, 2014, 4:50am UTC](https://discuss.kotlinlang.org/t/immutable-collections-perhaps-guava-integration-or-optional-extension-module/421/4 "2014-11-04T04:50:40Z")

</div>

There are plans for adding immutable/persistent collection classes in the future, but no concrete decisions are taken yet

---

<div class="post-metadata">

**Author:** ![rbygrave](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/rbygrave/32/1433_2.png) [@rbygrave](https://discuss.kotlinlang.org/u/rbygrave)\
**Post date:** [November 4, 2014, 5:13am UTC](https://discuss.kotlinlang.org/t/immutable-collections-perhaps-guava-integration-or-optional-extension-module/421/5 "2014-11-04T05:13:56Z")

</div>

Right thanks.&nbsp;&nbsp;Yes toSet() toList() does the trick - I just didn't find them.&nbsp;&nbsp;

Looking at the implementation for toList() …

&nbsp;&nbsp;public fun \<T\> Iterable\<T\>.toList(): List\<T\> {  
&nbsp;&nbsp;return toCollection(ArrayList\<T\>())  
&nbsp;&nbsp;}

… I wonder if the ImmutableArrayList implementation could be used instead of ArrayList so more like:

 &nbsp;&nbsp;public fun\<T\> MutableList\<T\>.toList() : List\<T\> {

```
  &nbsp;&nbsp;val array = this.copyToArray()
  &nbsp;&nbsp;return ImmutableArrayList(array, 0, array.size)

```

&nbsp;&nbsp;}

but yes, toSet() toList() is all good - thanks !!!

---

<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:** [November 4, 2014, 7:00am UTC](https://discuss.kotlinlang.org/t/immutable-collections-perhaps-guava-integration-or-optional-extension-module/421/6 "2014-11-04T07:00:20Z")

</div>

ImmutableArrayList is there for historical reasons only. We'll probably have it removed or reimplemented.
