Deep understanding of hashcode and equals

One, detailed explanation of hashcode

1. Hashcode() is used to return the hash code of the string, hashcode is used to find and use, and equals is used to compare whether two objects are equal.

class Test{
    public static void main(String[] args){
        String str = new String("www.baidu.com");
        System.out.print("字符串的哈希吗为:" + str.hashcode());
    }
}

 2. Hashcode() method: returns the hash value of the string object.

public int hashcode();

3. Hashcode is mainly used for the quickness of search. It is used to determine the storage address of the object in the hash storage structure, that is, the position of the object in the hash table. Physically, it can be said that the object is stored in the memory address.

4. If the two objects are the same, that is, the A.equals(B) method, then the hashcodes of the two objects of A and B must be the same.

5. If the hashcode method of the object is rewritten, the hashcode method of the object should also be rewritten. The resulting object must be consistent with the object used by the equals method.

6. The hashcode() of the two objects is the same, which does not necessarily mean that the two objects of the equals method are the same. It only means that the objects produced by the two objects in the hash storage structure are not equal.

7. Why is hashcode search faster?

     Now we want to store 1000 different numbers in the memory. The common method is to store a number, generally store one, and iterate over to see if there are the same numbers. If you store 10,000 different data, you have to traverse each, which is very time-complex and low-efficiency.
     Now we use hashcode to record the location of the object and find it.

     Hash storage: Assuming that there are 1, 2, 3, 4, 5, 6, 7 hash storage locations in the hash table, store the first number, the hashcode is 1, and the number is placed in position 1 in the hash table. When the number reaches 1000, there may be many numbers in the 7 positions in the hash table. There may be 200 numbers in position 1. When storing 1001 numbers, check the position of the hashcode object first, assuming that the corresponding position is 1, because the positions in the hash table are the same, and the hash values ​​are the same. 1001 only needs to compare the table It is enough if the 200 numbers in the middle are the same. If they are not the same as the 200 numbers, they are stored in the position of 1, which reduces the number of queries. That is, the hashcode codes are the same, and the corresponding objects are not necessarily the same.

2. Detailed explanation of equals and the connection between hashcode and equals

1. The equals method. The function of equals is to determine whether two objects are equal. If the object overrides the equals method, compare the contents of the two objects for equality. If it is not overwritten, compare the addresses of the two objects to see if they are the same, which is equivalent to "==".

2. "==" is to judge whether the two addresses are equal, and equals is suitable for comparing whether the content of the object is equal, provided that the hash table container is not used, such as HashSet, HashTable, HashMap.

3. What is the relationship between equals method and hashcode?

     ① When not creating a hash table corresponding to the class, equals is used to compare whether two objects are equal, so now it has nothing to do with hashcode()

     ② Create a hash table corresponding to the class, such as creating HashSet, HashTable, HashMap, etc., in this case:

               <1> If two objects are equal, then their hashcode() values ​​must be equal. The equality here is true returned by comparing two objects by equals.

               <2> If the hashcode values ​​of two objects are equal, they are not necessarily equal. Because in the hash table, the hashcode() is equal, that is, the hash values ​​of the two key-value pairs are equal, but equal hash values ​​do not necessarily result in equal key-value pairs, and hash conflicts will occur at this time.

Three, summary

      The Java foundation is really important. Go through the knowledge points one by one, and gradually become familiar with the basics and the underlying principles.

      Come on! adhere to!

     

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/Sunshineoe/article/details/111505051