PHP中文网2017-04-18 10:47:27
Common convention for implementing hashCode
methods
During the execution of the application, as long as the information used in the comparison operation of the object's
equals
method is not modified, then the same object is called multiple times, and theequals
方法的比较操作所用到的信息没有被修改,那么对这个同一对象调用多次,hashCode
method must consistently return the same integer. During multiple executions of the same application, the integers returned by each execution may be inconsistent.- method of two objects returns the same integer result, it does not mean that the two objects are equal, because the
If two objects are equal according to the
. On the contrary, if theequals(Object)
method, then calling theequals(Object)
方法比较是相等的,那么调用这两个对象中任意一个对象的hashCode
方法都必须产生同样的整数结果。反之,如果两个对象hashCode
方法返回整数结果一样,则不代表两个对象相等,因为equals
method of either object must produce the same integer resultequals
method can be overloaded.- method of either object does not necessarily produce different integer results. However, if you can make different objects produce different integer results, it is possible to improve the performance of the hash table.
If two objects are not equal according to the
equals(Object)
方法比较是不相等的,那么调用这两个对象中任意一个对象的hashCode
equals(Object)
method, then calling the
hashCode
Hash code calculation (from: Effective Java)
- .
Put a non-zero constant value, such as a variable of type
17
,保存在一个名为result
的int
and each field involved in the method
f
(指equals
For each key field in the object), complete the following steps:
Compute a hash code c of type
int
for this domain:
- ).
if the domain is
boolean
类型,则计算(f?1:0
- .
If the domain is
byte
,char
,short
或者int类型,则计算(int)f
- .
If the domain is
long
类型,则计算(int)(f^(f>>>32))
- .
If the domain is
float
类型,则计算Float.floatToIntBits(f)
- to get the
If the field is of
double
类型,则计算Double.doubleToLongBits(f)
,然后按照步骤2.1.3,为得到的long
double
type, calculateDouble.doubleToLongBits(f)
, and then follow step 2.1.3long
Type value computes a hash value.If the field is an object reference, and the
equals
方法通过递归地调用equals
的方式来比较这个域,则同样为这个域递归地调用hashCode
。如果需要更复杂的比较,则为这个域计算一个范式(canonical representation)
,然后针对这个范式调用hashCode
。如果这个域的值为null
,则返回0
equals
method of the class compares the field by recursively callingequals
, it is also called recursively for this field. 🎜. If a more complex comparison is required, compute a canonical representation(canonical representation)
for the domain and then call 🎜 against this paradigm. If the value of this field isnull
,0
is returned (other constants are also acceptable). 🎜If the field is an array, each element should be treated as a separate field. That is, apply the above rules recursively, calculate a hash code for each significant element, and then combine these hash values according to step 2.2. If every element in the array field is important, you can take advantage of one of the methods added in release 1.5
Arrays.hashCode
.According to the following formula, the hash code calculated in step 2.1
c
合并到result
中:result = 31 * result + c
; //此处31
is an odd prime number, and has a good feature, that is, using shifting and subtraction instead of multiplication, you can get better performance: `31* i == (i<<5) - i, modern JVM can automatically complete this optimization.Return
result
Verify and test whether the
hashCode
implementation conforms to common conventions.
Example implementation
@Override
public int hashCode() {
int result = 17;
result = 31 * result + (origin == null ? 0 : origin.hashCode());
result = 31 * result + (hsNumber == null ? 0 : hsNumber.hashCode());
result = 31 * result + (imageUrl == null ? 0 : imageUrl.hashCode());
result = 31 * result + (classificationName == null ? 0 : classificationName.hashCode());
return result;
}
大家讲道理2017-04-18 10:47:27
Java’s int is fixed to 32 bits. In addition, your latitude and longtitude are double... I think it will be 64-bit.
hashcode and equals have agreed semantics. You can take a look at Object
I think the equals you wrote can be used.
Note: The contract in the Object class is actually a very weak constraint. We can write hashcode() and equals() like this without violating the contract;
public int hashcode() {
return 0;
}
public boolean equals(Object o) {
return (o != null) && (o.getClass() == getClass());
}
So the real question is how you define equality. Code is secondary.
If equality is defined as "longitude and latitude are equal respectively", then the code you gave is a usable solution (but not the only available solution).