Caching hashes in Java collections?

前端 未结 5 366
一向
一向 2021-01-23 17:26

When I implement a collection that uses hashes for optimizing access, should I cache the hash values or assume an efficient implementation of hashCode()?

On

5条回答
  •  抹茶落季
    2021-01-23 18:05

    Considering that java.lang.String caches its hash, i guess that hashcode() is supposed to be fast.

    So as first approach, I would not cache hashes in my collection. In my objects that I use, I would not cache hash code unless it is oviously slow, and only do it if profiling tell me so.

    If my objects will be used by others, i would probubly consider cachnig hash codes sooner (but needs measurements anyway).

提交回复
热议问题