HashCode methods:
The hashCode method exists to support Hash containers, such as HashMap, HashSet, etc., which use hashCode to calculate the location of storage.
Add a custom class instance to a hash container:
If we want to add an instance object of a custom class to the Hash container:
- Added as a key to a HashMap: The key of a HashMap cannot be repeated
- Add to HashSet: A HashSet does not allow duplicate elements
- (In another secret, HashSet is implemented by HashMap)
Why rewrite hashCode:
If we want to implement 1 and 2, we need to rewrite these two methods. Why? The Hash container uses the Object’s hashCode to calculate where the Object is stored. Custom objects inherit the Object’s hashCode method and return a different hashCode method for each different Object. Person1 ={name: “zhang SAN “, age: 20}, and person2={name:” Zhang SAN “, age: 20}, if you want to put them both in the Hash container: map.put(person1,PRESENT); map.put(person2,PERSENT); Then they take up two Spaces, because their Hashcodes are different, so the Hash container will store them both in the container, but their attributes are exactly the same, which is logically wrong, so we need to rewrite hashCode so that objects with the same attributes have the same hashCode. Here we rewrite the hashCode method using the above example person as an example. It may not be appropriate to use the hashCode method in practice.
class Person {
private String name;
private int age;
// omit getter, setter, toString
// Associating hashCode with attributes ensures that objects with the same attributes have the same hashCode
public int hashCode(a){
int hash = 0;
if(this.name ! =null) {int temp = 1;
hash = hash<<4 + temp*this.name.hashCode();
hash = hash<<4 + temp*this.age;
}
returnhash; }}Copy the code
Why override equals:
So why override equals? Since using hashCode to calculate the storage location and memory address is different, there is no guarantee of uniqueness. When adding a custom object to a Hash container, two different objects may have the same Hash value. This is a Hash conflict. The Hash container chooses to use the chained address method to handle collisions. That is, there may be multiple objects stored in a linked list in one location. How do you identify the object to be queried? Equals equals equals equals equals equals equals equals equals equals equals equals
Equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals equals External queries can only succeed if they use objects already stored in the Hash container: it doesn’t make sense that I already have this object, so why should I look it up? To cut a dead end. The hash container determines that two classes are equal only if their attributes are equal. The default equals method doesn’t work, so we need to rewrite it. If using the above example to illustrate the equals method can write, so not suitable, the actual use of the time will be subject to your requirements:
class Person{
private String name;
private int age;
// omit getter, setter, toString
// Attribute is equal if it is equal, return true, otherwise return false
public boolean equals(Object obj){
if(obj == null) return false;
if(obj instanceOf Person) {
Person person = (Person) obj;
return obj.getName().equals(this.name) && obj.getAge()==this.age;
}
return false; }... }Copy the code
To sum up:
- The hashCode method exists to support Hash containers
- The Hash container uses hashCode to calculate the storage location, but the storage location calculated using hashCode is not guaranteed to be unique, and may cause Hash conflicts. In jdk7, Hash containers used linked lists to resolve Hash conflicts. (jdK8 later also involves red black tree, I am not familiar with now, so I will not embarrass)
- If the hashCode method is not overridden, two objects with the same attributes will have different Hashcodes, and the uniqueness requirement of the Hash container cannot be correctly implemented
- If you do not override equals, there is no way to determine the target element for multiple hashing elements stored in a linked list at the same location