# A HashMap is not a Map?

**URL:** <https://discuss.kotlinlang.org/t/a-hashmap-is-not-a-map/504>\
**Category:** Uncategorized\
**Created:** [August 23, 2014, 4:08pm UTC](https://discuss.kotlinlang.org/t/a-hashmap-is-not-a-map/504 "2014-08-23T16:08:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RobNik](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@RobNik](https://discuss.kotlinlang.org/u/RobNik)\
**Post date:** [August 23, 2014, 4:08pm UTC](https://discuss.kotlinlang.org/t/a-hashmap-is-not-a-map/504/1 "2014-08-23T16:08:13Z")

</div>

I thought this would work. Should it?

&nbsp;&nbsp;val m: java.util.Map\<String, Any\> = java.util.HashMap\<String, Any\>()

  
It gives this error:

&nbsp;&nbsp;Error:(21, 44) Kotlin: Type mismatch: inferred type is java.util.HashMap\<kotlin.String, kotlin.Any\>   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;but java.util.Map\<kotlin.String, kotlin.Any\> was expected

Kotlin version 0.8.11.

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:** [August 23, 2014, 4:30pm UTC](https://discuss.kotlinlang.org/t/a-hashmap-is-not-a-map/504/2 "2014-08-23T16:30:37Z")

</div>

I think, the compiler warns you against using java.util.Map: if you use kotlin.Map (or MutableMap) instead, it will work fine

---

<div class="post-metadata">

**Author:** ![RobNik](https://avatars.discourse-cdn.com/v4/letter/r/46a35a/32.png) [@RobNik](https://discuss.kotlinlang.org/u/RobNik)\
**Post date:** [August 23, 2014, 4:43pm UTC](https://discuss.kotlinlang.org/t/a-hashmap-is-not-a-map/504/3 "2014-08-23T16:43:08Z")

</div>

Okay, the error is gone. This seems like magic... is the magic explained somewhere? I looked at the reference doc for kotlin.Map and MutableMap and it's not inherting from java.util.Map. Are these types like aliases, or ?&nbsp;&nbsp;I can't assign one to a java.util.Map variable, but I can pass it to a java function that takes java.util.Map as a parameter. Usually those two things work the same.

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:** [August 23, 2014, 10:31pm UTC](https://discuss.kotlinlang.org/t/a-hashmap-is-not-a-map/504/4 "2014-08-23T22:31:14Z")

</div>

Some explanation is available here: [http://blog.jetbrains.com/kotlin/2012/09/kotlin-m3-is-out/](http://blog.jetbrains.com/kotlin/2012/09/kotlin-m3-is-out/)

In short, Java’s Map is mutable, and in Kotlin we replace it with a pair of interfaces (read-only) Map and MutableMap to facilitate better type safety. If these extended Java’s Map, they’d have to be mutable as well.
