Caching hashes in Java collections?

前端 未结 5 362
一向
一向 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 17:56

    I'd say in most cases you can rely on efficient implementations of hashCode(). AFAIK, that method is only invoked on lookup methods (like contains, get etc.) or methods that change the collection (add/put, remove etc.).

    Thus, in most cases there shouldn't be any need to cache hashes yourself.

提交回复
热议问题