# Total size of HashMap flatten elements

**URL:** <https://discuss.kotlinlang.org/t/total-size-of-hashmap-flatten-elements/22354>\
**Category:** Language Design\
**Created:** [July 8, 2021, 6:32pm UTC](https://discuss.kotlinlang.org/t/total-size-of-hashmap-flatten-elements/22354 "2021-07-08T18:32:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Psijic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/psijic/32/2227_2.png) [@Psijic](https://discuss.kotlinlang.org/u/Psijic)\
**Post date:** [July 8, 2021, 6:32pm UTC](https://discuss.kotlinlang.org/t/total-size-of-hashmap-flatten-elements/22354/1 "2021-07-08T18:32:05Z")

</div>

Hello, is there a `hashMapOf<String, MutableList<String>>()` method returns total size of all elements in O(1)? Guys [there](https://stackoverflow.com/questions/54760210/count-number-of-elements-in-a-mapstring-mapstring-int-by-its-keys) just like me still make it old style `forEach` way.

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [July 8, 2021, 6:57pm UTC](https://discuss.kotlinlang.org/t/total-size-of-hashmap-flatten-elements/22354/2 "2021-07-08T18:57:43Z")

</div>

There is nothing really “old” in using `forEach()` or loops. But if you are looking for functional way of doing this then:

`z.values.sumOf { it.size }`

If you’re looking for something similar to SO question, so create `Map<String, Int>` as a result then:

`z.mapValues { it.value.size }`

---

<div class="post-metadata">

**Author:** ![Psijic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/psijic/32/2227_2.png) [@Psijic](https://discuss.kotlinlang.org/u/Psijic)\
**Post date:** [July 8, 2021, 8:00pm UTC](https://discuss.kotlinlang.org/t/total-size-of-hashmap-flatten-elements/22354/3 "2021-07-08T20:00:27Z")

</div>

Hm, even if it’s not O(1), at least it’s a 1-string method. Is there a case this method is used often or it’s hard to implement this variable?

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [July 8, 2021, 8:07pm UTC](https://discuss.kotlinlang.org/t/total-size-of-hashmap-flatten-elements/22354/4 "2021-07-08T20:07:50Z")

</div>

I don’t think it is possible to do it in O(1). This data structure is basically a collection of collections, so we really need to iterate and count items. You can always create your own data structure that counts elements while adding them.

Sorry, I don’t understand the last sentence in your post.

---

<div class="post-metadata">

**Author:** ![Psijic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/psijic/32/2227_2.png) [@Psijic](https://discuss.kotlinlang.org/u/Psijic)\
**Post date:** [July 8, 2021, 8:18pm UTC](https://discuss.kotlinlang.org/t/total-size-of-hashmap-flatten-elements/22354/5 "2021-07-08T20:18:19Z")

</div>

I meant - can a new variable be implemented in a base `map` class - if it contains iterables, increase the total size of the `map` counting all its elements?

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [July 8, 2021, 9:23pm UTC](https://discuss.kotlinlang.org/t/total-size-of-hashmap-flatten-elements/22354/6 "2021-07-08T21:23:08Z")

</div>

You can create your own data structure that implements `MutableMap<String, MutableList<String>>` and does this.
