【问题标题】:Is there any reason to use Apache HashCodeBuilder over Objects.hash?有什么理由在 Objects.hash 上使用 Apache HashCodeBuilder?
【发布时间】:2012-11-29 20:09:25
【问题描述】:

我正在覆盖对象的 hashCode 和 equals 方法。我正在使用 Apache Commons 库中的 EqualsBuilder 来覆盖等于。由于我使用的是 Java 7,因此我计划使用内置的 hash method in the Objects class 而不是 Apache Commons HashCodeBuilder。这种方法有什么问题吗?有什么理由更喜欢其中一个吗?

【问题讨论】:

    标签: java hashcode


    【解决方案1】:

    HashCodeBuilder 具有时髦的反射哈希码实现,但除此之外,不 - 您可能应该使用 Objects

    【讨论】:

      【解决方案2】:

      一个区别是HashCodeBuilder 可以反射计算哈希码:

      public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
      }
      

      也就是说,使用HashCodeBuilder,您不必列出各个字段,尽管您可以。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-29
        • 2013-09-27
        • 2011-11-16
        相关资源
        最近更新 更多