# Hashcode in immutable data classes

**URL:** <https://discuss.kotlinlang.org/t/hashcode-in-immutable-data-classes/1844>\
**Category:** Language Design\
**Created:** [July 5, 2016, 12:24pm UTC](https://discuss.kotlinlang.org/t/hashcode-in-immutable-data-classes/1844 "2016-07-05T12:24:49Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![salomonbrys](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/salomonbrys/32/1445_2.png) [@salomonbrys](https://discuss.kotlinlang.org/u/salomonbrys)\
**Post date:** [July 5, 2016, 12:24pm UTC](https://discuss.kotlinlang.org/t/hashcode-in-immutable-data-classes/1844/1 "2016-07-05T12:24:49Z")

</div>

I would like to know if data classes that are immutable (containing only `val`) have `String` like optimized hashcode. I need said data classes to be as fast as possible as keys in a HashMap.

In other words, is it interresting to do this, or does the compiler do it for me?

```
data class Key(
        val bind: Bind, // This is also an immutable data type
        val argType: Type
) {
    private var _hashCode: Int = 0;

    override fun hashCode(): Int {
        if (_hashCode == 0)
            _hashCode = super.hashCode()
        return _hashCode
    }
}

```

---

<div class="post-metadata">

**Author:** ![udalov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/udalov/32/5679_2.png) [@udalov](https://discuss.kotlinlang.org/u/udalov)\
**Post date:** [July 5, 2016, 12:38pm UTC](https://discuss.kotlinlang.org/t/hashcode-in-immutable-data-classes/1844/2 "2016-07-05T12:38:38Z")

</div>

No, the hash code value is not stored in the implementation generated for the data classes. If that’s needed, the value should be stored manually.

---

<div class="post-metadata">

**Author:** ![salomonbrys](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/salomonbrys/32/1445_2.png) [@salomonbrys](https://discuss.kotlinlang.org/u/salomonbrys)\
**Post date:** [July 5, 2016, 1:08pm UTC](https://discuss.kotlinlang.org/t/hashcode-in-immutable-data-classes/1844/3 "2016-07-05T13:08:39Z")

</div>

Thanks 😉

---

<div class="post-metadata">

**Author:** ![salomonbrys](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/salomonbrys/32/1445_2.png) [@salomonbrys](https://discuss.kotlinlang.org/u/salomonbrys)\
**Post date:** [July 5, 2016, 1:37pm UTC](https://discuss.kotlinlang.org/t/hashcode-in-immutable-data-classes/1844/4 "2016-07-05T13:37:02Z")

</div>

@udalov: is the implementation that I’ve shown in my first message correct?

Will calling `super.hashCode()` will call the data class generated `hashCode` function or just `Any.hashCode`?

---

<div class="post-metadata">

**Author:** ![udalov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/udalov/32/5679_2.png) [@udalov](https://discuss.kotlinlang.org/u/udalov)\
**Post date:** [July 5, 2016, 2:09pm UTC](https://discuss.kotlinlang.org/t/hashcode-in-immutable-data-classes/1844/5 "2016-07-05T14:09:23Z")

</div>

No, `super.hashCode()` refers to the implementation in the super class (`Any` here). Unfortunately, there’s no way to reuse the automatically generated implementation: as soon as you declare your own `hashCode()` function, the implementation is not generated anymore. So you need to implement hashing logic manually from scratch, save the result of the computation to the backing property and call it in the `hashCode()` function body.

---

<div class="post-metadata">

**Author:** ![salomonbrys](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/salomonbrys/32/1445_2.png) [@salomonbrys](https://discuss.kotlinlang.org/u/salomonbrys)\
**Post date:** [July 5, 2016, 2:59pm UTC](https://discuss.kotlinlang.org/t/hashcode-in-immutable-data-classes/1844/6 "2016-07-05T14:59:08Z")

</div>

Issue opened here : [https://youtrack.jetbrains.com/issue/KT-12991](https://youtrack.jetbrains.com/issue/KT-12991) 🙂

---

<div class="post-metadata">

**Author:** ![kilink](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/kilink/32/1864_2.png) [@kilink](https://discuss.kotlinlang.org/u/kilink)\
**Post date:** [September 12, 2016, 8:08pm UTC](https://discuss.kotlinlang.org/t/hashcode-in-immutable-data-classes/1844/7 "2016-09-12T20:08:17Z")

</div>

It would be nice if Kotlin just implemented this optimization, so I wouldn’t need to implement it myself for every immutable data class I create (I think everywhere I’ve used data classes they have been effectively immutable). I suppose it would need to be opt-in somehow, as suggested in the related issue.

---

<div class="post-metadata">

**Author:** ![naxymatt](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/naxymatt/32/4184_2.png) [@naxymatt](https://discuss.kotlinlang.org/u/naxymatt)\
**Post date:** [October 28, 2018, 8:45am UTC](https://discuss.kotlinlang.org/t/hashcode-in-immutable-data-classes/1844/8 "2018-10-28T08:45:09Z")

</div>

Jumping in to note that this is a really big issue for the project I’m working on at the moment.

The project has a rather complex hierarchy of data classes (hundreds of them), all of which are immutable. Several of the data classes at the top of the hierarchy are stored in various `HashMap`s.

We’ve had to manually implement a caching version of `hashCode()` for many of the data classes because calculating it every time was causing severe performance problems (the app was unusable).

This makes the code quite awkward to maintain, and prone to hard-to-find bugs (forgetting to include a newly added field in a hash calculation can cause bugs that go undetected for a long time, and only pop up in the field).

Data classes are great solution aside from this (this hierarchy of nested immutable data classes greatly simplifies concurrency during document updates between the UI and the render thread in the app, and also made it trivially easy to implement lightweight infinite-level undo; not to mention being the core of the architecture for editing documents synced across different app instances via the cloud).

It would be really really nice if it were possible to annotate a data class as immutable so we get a cached hashCode implementation automatically.

---

<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 30, 2018, 12:20pm UTC](https://discuss.kotlinlang.org/t/hashcode-in-immutable-data-classes/1844/9 "2018-10-30T12:20:10Z")

</div>

I feel your pain. You want a way to get the hashcode calculated without doing it by hand or being stuck to the default implementation.I think a way to do this is using kotlin serialization. You can write a (simple) serializer that calculates the hashcode as “serialization” of the data. It comes with the normal serialization limitations, but for data classes it should be fine. You don’t even need to rely on reflection as serialization generates static code. As a side-effect your data classes now support other serialization as well.

---

<div class="post-metadata">

**Author:** ![stephendunn](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/stephendunn/32/6891_2.png) [@stephendunn](https://discuss.kotlinlang.org/u/stephendunn)\
**Post date:** [March 18, 2020, 12:33am UTC](https://discuss.kotlinlang.org/t/hashcode-in-immutable-data-classes/1844/10 "2020-03-18T00:33:35Z")

</div>

This implementation using 0 as a placeholder seems weird—0 is a valid hashcode. Would make more sense to use `Int?` and `null`. And why can’t you just use `lazy`?
