在调用 Objects.hash() 之前是否需要检查值是否为空?
Do I need to check if an value is null before calling Objects.hash()?
我有一个自定义对象,其属性包括可以是 null
的整数(例如 Integer num1
和 Integer num2
)。
对于相等函数,我通过 return num1 == customObj.num1 && num2 == customObj.num2
确定我的 2 个自定义对象是否相等。这个作品 b/c null == null
是 true
in Java。
所以我想知道在我的对象的哈希码函数中是否可以做 return Objects.hash(num1, num2)
?
我只是不确定我是否可以将 null
对象传递给 Objects.hash(...)
以及这是否会以某种方式弄乱哈希码或者我是否会得到 NullPointerException
.
您可以使用Objects#hashcode
如文档 Objects#hashcode 中所述,仅为非空参数生成 hash code
,如果参数为 null
,则生成 0。最终你永远得不到NullPointerException
.
Returns the hash code of a non-null argument and 0 for a null
argument.
如果你有多个字段,你可以使用它的重载版本 Objects#hash(Object... values)
根据文档
Generates a hash code for a sequence of input values. The hash code is
generated as if all the input values were placed into an array, and
that array were hashed by calling Arrays.hashCode(Object[]).
您测试相等性的方法已损坏。
虽然它在两个 Integer
对象都是 null
时有效,但当它们不为空时通常不起作用。您正在比较对象的引用而不是它们的数值。
使用Objects.equals()
to perform a null-safe test using the objects' equals()
method. While you're there, read the manual for Objects.hashCode()
.
您必须通过文档进行一些挖掘,以了解如果这些引用之一为 null 时的行为。
The hash code is generated as if all the input values were placed into an array, and that array were hashed by calling Arrays.hashCode(Object[])
.
The value returned by this method is equal to the value that would be returned by Arrays.asList(a).hashCode()
Arrays.asList(Object[])
returns一个List
,List.hashCode()
表示:
The hash code of a list is defined to be the result of the following calculation:
int hashCode = 1;
for (E e : list)
hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
因此,空元素的处理方式与哈希码为零的对象相同。
虽然需要进行一些挖掘才能得出这个答案,但更简单的策略是直接尝试一下——这样你可以更快地得到答案。
我有一个自定义对象,其属性包括可以是 null
的整数(例如 Integer num1
和 Integer num2
)。
对于相等函数,我通过 return num1 == customObj.num1 && num2 == customObj.num2
确定我的 2 个自定义对象是否相等。这个作品 b/c null == null
是 true
in Java。
所以我想知道在我的对象的哈希码函数中是否可以做 return Objects.hash(num1, num2)
?
我只是不确定我是否可以将 null
对象传递给 Objects.hash(...)
以及这是否会以某种方式弄乱哈希码或者我是否会得到 NullPointerException
.
您可以使用Objects#hashcode
如文档 Objects#hashcode 中所述,仅为非空参数生成 hash code
,如果参数为 null
,则生成 0。最终你永远得不到NullPointerException
.
Returns the hash code of a non-null argument and 0 for a null argument.
如果你有多个字段,你可以使用它的重载版本 Objects#hash(Object... values)
根据文档
Generates a hash code for a sequence of input values. The hash code is generated as if all the input values were placed into an array, and that array were hashed by calling Arrays.hashCode(Object[]).
您测试相等性的方法已损坏。
虽然它在两个 Integer
对象都是 null
时有效,但当它们不为空时通常不起作用。您正在比较对象的引用而不是它们的数值。
使用Objects.equals()
to perform a null-safe test using the objects' equals()
method. While you're there, read the manual for Objects.hashCode()
.
您必须通过文档进行一些挖掘,以了解如果这些引用之一为 null 时的行为。
The hash code is generated as if all the input values were placed into an array, and that array were hashed by calling
Arrays.hashCode(Object[])
.
The value returned by this method is equal to the value that would be returned by
Arrays.asList(a).hashCode()
Arrays.asList(Object[])
returns一个List
,List.hashCode()
表示:
The hash code of a list is defined to be the result of the following calculation:
int hashCode = 1; for (E e : list) hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
因此,空元素的处理方式与哈希码为零的对象相同。
虽然需要进行一些挖掘才能得出这个答案,但更简单的策略是直接尝试一下——这样你可以更快地得到答案。