First, pay tribute to the author:
http://blog.csdn.net/fenglibing/article/details/8905007
The following is an official document definition for hashcode:
- The Hashcode method returns the hash code value of the object. This method is supported to provide some advantages to the hash table, for example, the Hashtable provided by Java.util.Hashtable.
- The general agreement of Hashcode is:
- During Java application execution, when the Hashcode method is called multiple times on the same object, the same integer must be returned consistently, provided that the information used in the Equals comparison on the object has not been modified. The integer does not need to be consistent from one execution of an application to another execution of the same application.
- If two objects are equal according to the Equals (object) method, calling the Hashcode method on each object in two objects must produce the same integer result.
- The following conditions are not required: If two objects are not equal according to the Equals (Java.lang.Object) method, then calling the Hashcode method on any of the two objects must produce a different integer result. However, programmers should know 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 converting the object's internal address to an integer, but the JAVATM programming language does not require this implementation technique.) )
- When the Equals method is overridden, it is often necessary to override the Hashcode method to maintain the general contract of the Hashcode method, which declares that the equality object must have an equal hash code.
The above-mentioned official document definition, we can be drawn into the following key points:
1, the existence of hashcode is mainly used to find the fast, such as Hashtable,hashmap, Hashcode is used in the hash storage structure to determine the storage address of the object;
2, if two objects are the same, is applicable to the Equals (Java.lang.Object) method, then the two objects must be the same hashcode;
3, if the object's Equals method is overridden, then the object's hashcode also try to rewrite, and produce hashcode used objects, must be consistent with the use of the Equals method, otherwise it will violate the above mentioned 2nd;
4, two objects of the same hashcode, does not necessarily mean that two objects are the same, that is not necessarily applicable to the Equals (Java.lang.Object) method, only can explain that the two objects in the hash storage structure, such as Hashtable, they " stored in the same basket. "
Again, the hashcode is used for lookup purposes, and equals is used to compare the equality of two objects. The following is a copy of the message from someone else's post:
- 1.hashcode is used to find, if you have learned the data structure you should know that in the Find and sort this chapter has
- For example, there is such a location in memory
- 0 1 2 3 4 5 6 7
- And I have a class, this class has a field called ID, I want to put this class in one of the above 8 locations, if not hashcode and arbitrary storage, then when looking for the need to go to these eight locations to find, or using a two-way algorithm.
- But if you use hashcode that will improve the efficiency a lot.
- We have a field called ID in this class, then we define our hashcode as id%8, and then we store our class in the place where we get the remainder. For example, our ID is 9, 9 except 8 of the remainder is 1, then we put the class exists 1 this position, if the ID is 13, the remainder is 5, then we put the class at 5 this position. In this way, the remainder can be found directly by ID in addition to 8 when the class is looked up.
- 2. But if the two classes have the same hashcode what to do (we assume that the ID of the class above is not unique), such as 9 divided by 8 and 17 divided by 8, the remainder is 1, then this is not legal, the answer is: can do so. So how do you judge it? At this point, you need to define equals.
- In other words, we first determine whether two classes are stored in a bucket by hashcode, but there may be many classes in the bucket, then we need to find our class in this bucket by equals.
- So. overriding Equals (), why rewrite Hashcode ()?
- If you want to find something in a bucket, you have to find the bucket first, you don't have to rewrite hashcode () to find the bucket, the Light rewrite equals () What's the use?
Finally, let's look at a concrete example,
Public classHashtest {Private inti; Public intGeti () {returni; } Public voidSetI (inti) { This. i =i; } Public inthashcode () {returnI% 10; } Public Final Static voidMain (string[] args) {hashtest a=Newhashtest (); Hashtest b=Newhashtest (); A.seti (1); B.seti (1); Set<HashTest> set =NewHashset(); Set.add (a); Set.add (b); System.out.println (A.hashcode ()==B.hashcode ()); System.out.println (A.equals (b)); SYSTEM.OUT.PRINTLN (set); }}
The result of this output:
- True
- False
- [[Email protected], [email protected]]
The above example, we just rewrite the Hashcode method, from the above results can be seen, although the two objects hashcode equal, but actually two objects are not equal, we did not rewrite the Equals method, then the object is called the default Equals method, Is the comparison of two objects is not the same, showing that this is two different objects, two object references must be indeterminate. Here we put the generated object into the HashSet, and HashSet only can hold a unique object, that is, the same (for the Equals method) of the object will only hold one, but here is actually two objects, a, a, a, is put in the HashSet, So the HashSet lost his own meaning.
At this point we add the Equals method:
Public classHashtest {Private inti; Public intGeti () {returni; } Public voidSetI (inti) { This. i =i; } Public Booleanequals (Object object) {if(Object = =NULL) { return false; } if(Object = = This) { return true; } if(! (Objectinstanceofhashtest)) { return false; } hashtest Other=(hashtest) object; if(Other.geti () = = This. Geti ()) { return true; } return false; } Public inthashcode () {returnI% 10; } Public Final Static voidMain (string[] args) {hashtest a=Newhashtest (); Hashtest b=Newhashtest (); A.seti (1); B.seti (1); Set<HashTest> set =NewHashset(); Set.add (a); Set.add (b); System.out.println (A.hashcode ()==B.hashcode ()); System.out.println (A.equals (b)); SYSTEM.OUT.PRINTLN (set); }}
The result will be as follows:
- True
- True
- [[email protected]]
As we can see from the results, two objects are now completely equal, and only one copy of the object is stored in the hashset.
Instructions for turning hashcode