Common convention for implementing hashCodemethods
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 the equals方法的比较操作所用到的信息没有被修改,那么对这个同一对象调用多次,hashCodemethod must consistently return the same integer. During multiple executions of the same application, the integers returned by each execution may be inconsistent.
If two objects are equal according to the equals(Object) method, then calling the equals(Object)方法比较是相等的,那么调用这两个对象中任意一个对象的hashCode方法都必须产生同样的整数结果。反之,如果两个对象hashCode方法返回整数结果一样,则不代表两个对象相等,因为equalsmethod of either object must produce the same integer result
. On the contrary, if the
method of two objects returns the same integer result, it does not mean that the two objects are equal, because the equals method can be overloaded.
equals(Object)方法比较是不相等的,那么调用这两个对象中任意一个对象的hashCode
If two objects are not equal according to the equals(Object) method, then calling the
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.
hashCode
Hash code calculation (from: Effective Java)
17,保存在一个名为result的int
Put a non-zero constant value, such as a variable of type
.
f(指equalsFor each key field in the object
and each field involved in the method ), complete the following steps:
If the field is an object reference, and the equals method of the class compares the field by recursively calling equals, 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 is null, 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.5Arrays.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.
Returnresult
Verify and test whether the hashCodeimplementation 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;
}
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).
Common convention for implementing
hashCode
methodshashCode
Example implementation
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;
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).