Use Objects.hash() or own hashCode() implementation?

前端 未结 5 1565
清酒与你
清酒与你 2021-01-01 08:24

I have recently discovered the Objects.hash() method.

My first thought was, that this tidies up your hashCode() implementation a lot. See the following

5条回答
  •  遥遥无期
    2021-01-01 09:07

    Note that the parameter of Objects.hash is Object.... This has two main consequences:

    • Primitive values used in the hash code calculation have to be boxed, e.g. this.id is converted from long to Long.
    • An Object[] has to be created to invoke the method.

    The cost of creating of these "unnecessary" objects may add up if hashCode is called frequently.

提交回复
热议问题