具有泛型类型的哈希码

Hashcode with generic types

寻找一种方法来轻松计算任意类型的哈希值。鉴于以下 class:

class Foo<T> {
    ...
    T value;
}

我已经使用 Objects.deepEquals 覆盖了 equals 方法来比较 value 字段。有没有一种简单的方法可以对 hashCode 做同样的事情? guava 或 apache commons 中的一些实用方法(我已经将它们用于其他用途)?

不幸的是,Objects.hash(field1, ... , value) 当值为数组时不起作用。

我知道一种选择是

Arrays.deepHashCode(new Object[]{ field1, ... , value });

但是为此创建一个新数组而不是循环它感觉不对

HashCodeBuilder.reflectionHashCode(object) 在 commons-lang3.

https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/builder/HashCodeBuilder.html