# Add possibility to cast EmptyMap to empty instance of any Map realization

**URL:** https://discuss.kotlinlang.org/t/add-possibility-to-cast-emptymap-to-empty-instance-of-any-map-realization/5138
**Category:** Language Design
**Created:** [October 27, 2017, 12:31pm UTC](https://discuss.kotlinlang.org/t/add-possibility-to-cast-emptymap-to-empty-instance-of-any-map-realization/5138 "2017-10-27T12:31:47Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![rjhdby](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/rjhdby/32/3868_2.png) [@rjhdby](https://discuss.kotlinlang.org/u/rjhdby)
#### Post date: [October 27, 2017, 12:31pm UTC](https://discuss.kotlinlang.org/t/add-possibility-to-cast-emptymap-to-empty-instance-of-any-map-realization/5138/1 "2017-10-27T12:31:47Z")

</div>

```
val hashMap = HashMap<String, String>()
//hashMap.put("key", "value") <--------------
val resultHashMap = hashMap.keySet()
    .map{doSomeWorkReturningListOfpairs()}
    .toMap() as HashMap<String, SomeObject>

```

If I uncomment `hashMap.put("key", "value")` line then this code correctly return `HashMap` object, but with empty `hashMap` this code throw exception

> kotlin.collections.EmptyMap cannot be cast to java.util.HashMap

And Idea does not shows any warning about this unknown behavior of `toMap` realization

---

<div class="post-metadata">

### Author: ![jstuyts](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jstuyts/32/2141_2.png) [@jstuyts](https://discuss.kotlinlang.org/u/jstuyts)
#### Post date: [October 27, 2017, 12:59pm UTC](https://discuss.kotlinlang.org/t/add-possibility-to-cast-emptymap-to-empty-instance-of-any-map-realization/5138/2 "2017-10-27T12:59:49Z")

</div>

You cannot rely on the type of collection that will be returned by the map operations. If you need a mutable map, replace `toMap()` with [`toMutableMap()`](http://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/to-mutable-map.html). Note that this will return a copy.

---

<div class="post-metadata">

### Author: ![rjhdby](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/rjhdby/32/3868_2.png) [@rjhdby](https://discuss.kotlinlang.org/u/rjhdby)
#### Post date: [October 27, 2017, 1:10pm UTC](https://discuss.kotlinlang.org/t/add-possibility-to-cast-emptymap-to-empty-instance-of-any-map-realization/5138/3 "2017-10-27T13:10:47Z")

</div>

If I can’t rely on return of a function why it function for?

It absurd situation what on same input object with difference only in elements count I receive two incompatible returns.

However Idea may warning about dangerous cast, but did not do it

---

<div class="post-metadata">

### Author: ![jstuyts](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jstuyts/32/2141_2.png) [@jstuyts](https://discuss.kotlinlang.org/u/jstuyts)
#### Post date: [October 27, 2017, 1:17pm UTC](https://discuss.kotlinlang.org/t/add-possibility-to-cast-emptymap-to-empty-instance-of-any-map-realization/5138/4 "2017-10-27T13:17:19Z")

</div>

> [@rjhdby](#):
>
> If I can’t rely on return of a function why it function for?

Okay, maybe this is clearer: You cannot rely on the actual type that is being returned. The only thing that is guaranteed to work is the declared return type of the function.

The reason you may get different implementations as the final result, is because your start situation is different: an empty map versus a map with at least 1 element.

---

<div class="post-metadata">

### Author: ![rjhdby](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/rjhdby/32/3868_2.png) [@rjhdby](https://discuss.kotlinlang.org/u/rjhdby)
#### Post date: [October 27, 2017, 1:49pm UTC](https://discuss.kotlinlang.org/t/add-possibility-to-cast-emptymap-to-empty-instance-of-any-map-realization/5138/5 "2017-10-27T13:49:38Z")

</div>

We talk about different things.

You talk about how it actually work, but I talk about changing behavior of `EmptyMap`.

It work this way becouse it work this way - is not an argument.

EmptyMap - is an instance of Map with zero elements. Why I can’t simply cast it to another Map realization with zero elements?

---

<div class="post-metadata">

### Author: ![jstuyts](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jstuyts/32/2141_2.png) [@jstuyts](https://discuss.kotlinlang.org/u/jstuyts)
#### Post date: [October 27, 2017, 2:27pm UTC](https://discuss.kotlinlang.org/t/add-possibility-to-cast-emptymap-to-empty-instance-of-any-map-realization/5138/6 "2017-10-27T14:27:46Z")

</div>

Yes, I talk about how it actually works, because it was designed to work this way. So, it is not possible to make random changes to the design because it does not fit your particular scenario. In this case, the design choice was to let the collection operators always return immutable collections.

Why is operating on immutable collections a good thing? Because the operators can then return optimized implementations of the collections, and these optimized collections may have very specific implementations for the operators that get called upon them. This (can) result in more efficient code in terms of memory and/or speed.

It is something you will have to live with. If you want to do it more efficiently, or you are forced to work with types that the standard functions do not return, you can either do it inefficiently by creating copies or you write code to do it in the way that you need.

For example, this is efficient and works with `HashMap`:

```kotlin
val sourceMap = HashMap<String, String>()
// Do whatever you want here: Leave it empty, fill it to the brim, etc.
val destinationMap = HashMap<String, SomeObject>()
sourceMap.keys.forEach { key ->
    destinationMap.put(key, doSomeWorkReturningSomeObjectToAssociateWith(key))
}

```

---

<div class="post-metadata">

### Author: ![ilogico](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilogico/32/3725_2.png) [@ilogico](https://discuss.kotlinlang.org/u/ilogico)
#### Post date: [October 27, 2017, 6:58pm UTC](https://discuss.kotlinlang.org/t/add-possibility-to-cast-emptymap-to-empty-instance-of-any-map-realization/5138/7 "2017-10-27T18:58:55Z")

</div>

If the function signature of `toMap()` states it will return a Map, you cannot assume it will return a `HashMap`. You can only assume it will return a `Map`. As it happens, the current implementation sometimes returns a `HashMap`, but it’s wrong to rely on it.

If you need a `MutableMap`, you should use `toMudatbleMap()` instead. There is rarely a reason to depend on a particular implementation rather than on an interface.

But if you absolutely need it to be a `HashMap`, you can create one and then collect the values into it like this: `.mapTo(HashMap<String, SomeObject>())` or mapValuesTo(HashMap\<String, SomeObject\>())`.

---

<div class="post-metadata">

### Author: ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)
#### Post date: [October 27, 2017, 7:42pm UTC](https://discuss.kotlinlang.org/t/add-possibility-to-cast-emptymap-to-empty-instance-of-any-map-realization/5138/8 "2017-10-27T19:42:31Z")

</div>

> [@rjhdby](#):
>
> EmptyMap - is an instance of Map with zero elements. Why I can’t simply cast it to another Map realization with zero elements?

What you want coersion or automatic conversion. That is not part of the Kotlin language.

The reason why the program may want to return different types is because doing so may be more efficient in various ways. The whole point of the use of interfaces is that the using program cannot rely on the specific return type so the implementation can choose the best. Of course nothing stops you from creating your own `toHashMap` extension function that explicitly returns a hashmap.
