Returns the hash code value of the object. This method is supported to improve the performance of a hash table (for example, java.util.Hashtable a hash table provided).
hashCodeThe General agreement is:
1. During Java application execution, when calling the hashcode method multiple times on the same object, the same integer must be returned consistently, provided is a
the object is equals the information used in the comparison has not been modified. From one execution of an application to another execution of the same application,
The integer does not need to be consistent.
2, if according to equals (Object) method, two objects are equal, and each object in the two objects is called hashCode methods must be born
Into the same integer result.
3, if according to equals(java.lang.Object) method, two objects are not equal, then a call to any of these two objects hashcode
Method No requires that a different integer result must be generated. However, programmers should be aware that generating different integer results for unequal objects can improve
The performance of the hash table.
In fact, the Hashcode method defined by the object class does return different integers for different objects. (This is typically done by adding the object
's internal address is translated into an integer, but the JavaTM programming language does not require this implementation technique. )
In real-world development, The Equals () method is overridden in addition to the Hascode () method.
Let's take a look at the Equals () method in the source code:
public boolean equals (Object obj) { return (this = = obj); }
we can see from the above procedure that the Equals () method is compared to an object and is equal.
equalsmethod to implement an equality relationship on a non-null object reference:
1. reflexive Nature : For any non-null reference value x , x.equals(x) should all return true .
2. symmetry of : For any non-null reference value x and the y , when and only if y.equals(x) return true when the x.equals(y) should not return true .
3. transitivity : For any non-null reference value x , y and the z , if x.equals(y) return true , and y.equals(z) return true , thenx.equals(z)
should return true .
4. Consistency : For any non-null reference value x and the y , multiple calls x.equals (y) always return true or always returns false , provided that the object
equalsThe information used in the comparison has not been modified.
5. For any non-null reference value x , x.equals(null) should all return false .
ObjectThe equals method of a class implements the most differentiated equality relationship on an object, that is, for any non-null reference value x and y , if and only ifx
yThis method returns true ( x == y with a value) when the same object is referenced true .
Note: When this method is overridden, it is often necessary to override the Hashcode method to maintain the general contract of the hashcode method, which declares equality
The object must have an equal hash code.
See the following procedure for how to override the Equals () and Hashcode () methods:
Class User { private String name; Protected String GetName () { return name; } public void SetName (String name) { this.name = name; } @Override public boolean equals (Object obj) { if (obj = = NULL | |! ( obj instanceof User) { return false; } else{ return true; } } @Override public int hashcode () { return; }} public class Main {public static void main (string[] args) {User U1 = new User (); U1.setname ("GU"); User U2 = new user (); U2.setname ("Bill"); System.out.println (U1.equals (U2));}}
Output Result:
True
In the above program, the Equals () method and the Hashcode () method are overridden in the user class.
Reprint Please specify source:http://blog.csdn.net/hai_qing_xu_kong/article/details/43899101 Emotional Control _