Java - 整数变量中的 hashCode() 返回分配的值
Java - hashCode() in Integer variable returning the assigned value
您好,我只是想了解一些基本概念,例如 java
编程中的 immutable
。所以我遇到了这个编码。
public class IntImmutable {
public static void main(String[] args) {
Integer i = 10;
System.out.println("value = "+i " hashCode = "+i.hashCode())
i+=9;
System.out.println("value = "+i " hashCode = "+i.hashCode())
}
}
这里我理解了代码概念immutable
但是hashCode()
returning的值很奇怪。这是输出
value = 10 hashCode = 10
value = 19 hashCode = 19
如果您注意到值和 hashCode
值相同,是否符合预期
行为,我的理解是 hashCode
会 return 一些 memory reference value
.
哈希码很好,因为它返回的值与 contract for hashCode.
一致
Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
是的,行得通。 i
的重新分配会创建一个新的 Integer 对象,因此哈希码可以不同。
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
使哈希码只是整数值是一种简单的方法。如果 equals(anotherInteger)
returns 为真,则这两个整数具有相同的值。使哈希码具有相同值的一种简单方法是使哈希码只是那些整数。
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
确实会发生这种情况,因为可能的 Integer
值集与可能的哈希码集相同。 java.lang.Integer
的哈希码实现方式在两个集合之间建立了简单的一对一映射。
i+=9
正在将 java.lang.Integer
的 new 实例分配给 i
。首先,i
被拆箱为原始值10,在该值上加上9得到19,然后重新装箱成不同的Integer
目的。 运行 以下代码:
Integer i = 10;
Integer j = 10;
System.out.println("value = " + i + " hashCode = " + i.hashCode());
System.out.println("value = " + j + " hashCode = " + j.hashCode());
i += 9;
System.out.println("value = " + i + " hashCode = " + i.hashCode());
System.out.println("value = " + j + " hashCode = " + j.hashCode());
来自Integer.hashCode()
documentation:
Returns:
a hash code value for this object, equal to the primitive int value represented by this Integer object.
hashCode()
合同声明它必须 return 一个整数值,使得:
If two objects are equal according to the equals(Object)
method, then calling the hashCode
method on each of the two objects must produce the same integer result.
我们可以看到,Integer.hashCode()
的实现满足了这个条件。
在您的例子中,哈希码是根据整数 class 本身计算得出的。
对于特定对象的纯哈希码,请使用以下代码。
System.identityHashCode(obj)
您好,我只是想了解一些基本概念,例如 java
编程中的 immutable
。所以我遇到了这个编码。
public class IntImmutable {
public static void main(String[] args) {
Integer i = 10;
System.out.println("value = "+i " hashCode = "+i.hashCode())
i+=9;
System.out.println("value = "+i " hashCode = "+i.hashCode())
}
}
这里我理解了代码概念immutable
但是hashCode()
returning的值很奇怪。这是输出
value = 10 hashCode = 10
value = 19 hashCode = 19
如果您注意到值和 hashCode
值相同,是否符合预期
行为,我的理解是 hashCode
会 return 一些 memory reference value
.
哈希码很好,因为它返回的值与 contract for hashCode.
一致Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
是的,行得通。 i
的重新分配会创建一个新的 Integer 对象,因此哈希码可以不同。
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
使哈希码只是整数值是一种简单的方法。如果 equals(anotherInteger)
returns 为真,则这两个整数具有相同的值。使哈希码具有相同值的一种简单方法是使哈希码只是那些整数。
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
确实会发生这种情况,因为可能的 Integer
值集与可能的哈希码集相同。 java.lang.Integer
的哈希码实现方式在两个集合之间建立了简单的一对一映射。
i+=9
正在将 java.lang.Integer
的 new 实例分配给 i
。首先,i
被拆箱为原始值10,在该值上加上9得到19,然后重新装箱成不同的Integer
目的。 运行 以下代码:
Integer i = 10;
Integer j = 10;
System.out.println("value = " + i + " hashCode = " + i.hashCode());
System.out.println("value = " + j + " hashCode = " + j.hashCode());
i += 9;
System.out.println("value = " + i + " hashCode = " + i.hashCode());
System.out.println("value = " + j + " hashCode = " + j.hashCode());
来自Integer.hashCode()
documentation:
Returns: a hash code value for this object, equal to the primitive int value represented by this Integer object.
hashCode()
合同声明它必须 return 一个整数值,使得:
If two objects are equal according to the
equals(Object)
method, then calling thehashCode
method on each of the two objects must produce the same integer result.
我们可以看到,Integer.hashCode()
的实现满足了这个条件。
在您的例子中,哈希码是根据整数 class 本身计算得出的。 对于特定对象的纯哈希码,请使用以下代码。
System.identityHashCode(obj)