First understand some of the underlying data structure dependent hash Series
Series hash data structures used to build the object which has at the bottom a hashcode index assigned to the respective position of the table. If the hash value of the element of the same, with a chain structure which is appended to the element group index has.
When we use the key index, the data structure is to actually get hashcode this key and then go to the index of the index data (chain) until the end.
Why Override Hashcode
Sentence, because we want to ensure that, by using hash values in our ADT as part of the data structure hashset etc. (the vast majority of cases this is not to be avoided), to avoid default hash value (the default is address, unpredictable) and we determined that two objects are equivalent inconsistent caused by unpredictable errors after using the constructed hash structure triggered.
, That is, we want to ensure that when we think of two objects are equal, we have to ensure that their hashcode is the same.
So that the two objects are considered to have an equivalent in nature, have the same hashcode, meet this principle, we can guarantee the correctness of the program running, our hashcode is a right . In other words, whether you combine prime resetting a mapping coding, or that you simply direct return like "42" This constant (42 is claimed to be the answer to everything, it is clear that combined experimental guide book "DON 'T PANIC ", an author of the Hitchhiker's Guide to the galaxy must be a diehard powder) are in line with our requirements.
hashcode equivalent objects must be equal, hashcode inequivalent preferably the same object
While we can make all the elements have the same hash value, (that they will form a long chain on one index), but this will be a significant impact on various properties. (Adding, indexes, etc.)
Simple verification experiment
Return different values for the object code is not equivalent
package P2;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class PhoneNumber {
public PhoneNumber(int areaCode,int prefix,int lineNumber){
this.areaCode = areaCode;
this.prefix = prefix;
this.lineNumber = lineNumber;
}
private final int areaCode;
private final int prefix;
private final int lineNumber;
@Override
public int hashCode() {
int[] hashArray = {areaCode, prefix, lineNumber};
return Arrays.hashCode(hashArray);
}
public static void main(String[] args) {
}
}
All objects returns the same value code
package P2;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class PhoneNumber {
public PhoneNumber(int areaCode,int prefix,int lineNumber){
this.areaCode = areaCode;
this.prefix = prefix;
this.lineNumber = lineNumber;
}
private final int areaCode;
private final int prefix;
private final int lineNumber;
@Override
public int hashCode() {
return 42;
}
public static void main(String[] args) {
}
}
Test code (data amount 10000)
package P2;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
public class phoneNumberTest {
@Test(expected = AssertionError.class)
public void testAssertionsEnabled() {
assert false;
}
@Test
public void testRuntime() {
Set<PhoneNumber> set = new HashSet<PhoneNumber>();
for(int i= 0;i<=10000;i++) {
PhoneNumber testNumber = new PhoneNumber(i,i,i);
set.add(testNumber);
}
}
}
Test results
can be seen, return undifferentiated hashcode have a significant impact on performance.
summary
When Override equals method must be accompanied override hashcode (unless you want to reduce the quality of your ADT default it will not participate in the construction of any structure hash series); hashcode to ensure that the code is equivalent to the object, it is best to ensure that no equivalence object code is not the same.