Understanding of HashCode and equal methods

The role of hashCode()

1.Hashcode is used to find, if you have studied data structure, you should know that in the chapter of finding and sorting, there
are such locations in memory as
0 1 2 3 4 5 6 7    
and I have a class, this class There is a field called ID. I want to store this class in one of the above 8 locations. If it is stored arbitrarily without hashcode, then when searching, I need to go to these eight locations to find one by one, or use a dichotomous method. algorithm.
But if you use hashcode, it will improve the efficiency a lot.
There is a field in our class called ID, then we define our hashcode as ID% 8, and then store our class in the position where the remainder is obtained. For example, if our ID is 9, and the remainder of 9 divided by 8 is 1, then we put the class in the position of 1. If the ID is 13 and the remainder is 5, then we put the class in the position of 5. In this way, when looking for this class in the future, you can directly find the storage location by dividing the ID by 8 to find the remainder.

2. But what if two classes have the same hashcode (we assume that the ID of the above class is not unique), for example, the remainder of 9 divided by 8 and 17 divided by 8 is 1, then this is not legal, The answer is: yes. So how to judge? At this time, you need to define equals.
That is to say, we first use hashcode to determine whether two classes are stored in a bucket, but there may be many classes in this bucket, then we need to use equals to find the class we want in this bucket.
So. Overriding equals(), why rewrite hashCode()?
Think about it, if you want to find something in a bucket, you must first find the bucket, you don't find the bucket by rewriting hashcode(), what's the use of just rewriting equals()
3. If you want to sort class A, there are two ways. One is to let class A implement the compareTo() method and implement the compareTo() method, then you can sort it through Collections.sort(List list).
Another way: define a Class B implements the Comparator class and implements the compare method, and then sorts by Collections.sort(List list, B b)

hashCode() is used to generate Hashma, and Hashma is used to determine the storage address of the object in the hash storage structure, (this paragraph is very clear in the Java programming thought) as in the util package The collection classes with hash all use this storage structure: HashMap, HashSet, when they store objects (strictly speaking, object references), they need to determine their addresses, and HashCode() is used for this purpose, generally It needs to be redefined, because by default, the hashCode method defined by the Object class will return different integers for different objects, which is generally achieved by converting the internal address of the object into an integer, now for example For example, take HashSet as an example, when an object is stored in it, the storage address of the object in the HashSet is determined by the hashCode() of the stored object, and the stored object is determined by equals() whether it is duplicate, hashCode () and equals() need to be redefined by themselves, because hashCode() has already been said by default, and equals() defaults to the comparison object reference. Think about it now, if you do not define equals(), then the same Two objects with exactly the same content generated by the class can be stored in Set, because they are determined by equals(), which makes HashSet lose its meaning.

Summarize:

The hashCode() method is used to improve the search efficiency in the Map. The Map will be placed in different buckets according to different hashCode(). When the Map searches for an object, it first finds the corresponding bucket through hashCode(), and then according to The equals() method finds the corresponding object. To correctly implement the search element in the Map, the following two conditions must be met:
(1) When obj1.equals(obj2) is true, obj1.hashCode() == obj2.hashCode() must true
(2) obj.equals(obj2) must be false when obj1.hashCode() == obj2.hashCode() is false

There are two types of collections in Java, one is List and the other is Set. Do you know the difference between them? The elements in the former set are ordered, and the elements can be repeated; the latter elements are unordered, but the elements cannot be repeated.
Then there is a more serious problem here: in order to ensure that elements are not repeated, what should be used to judge whether two elements are repeated? This is the Object.equals method.
However, if you check every time you add an element, then when there are many elements, the number of comparisons for the elements added to the collection is very high.
That is, if the collection now has 1000 elements, then when the 1001st element is added to the collection, it will call the equals method 1000 times. This obviously reduces efficiency considerably.
Hash algorithm, also known as hash algorithm, directly assigns data to an address according to a specific algorithm. We can think that the hashCode method returns the physical address of the object storage (actually it may not be, for example: by obtaining the physical address of the object and then dividing by 8 and then calculating the remainder, the remainder is the calculated hash value, we think it returns a not a numeric value of a physical address, but a value that can be mapped to a physical address).
In this way, when a new element is to be added to the collection, the hashCode method of this element is called first, and the physical position where it should be placed can be located at once. If there is no element in this position, it can be stored directly in this position without any further comparison; if there is already an element in this position, call its equals method to compare with the new element, if it is the same, it will not exist If not, hash other addresses if they are not the same. So there is a conflict resolution problem here. In this way, the actual number of calls to the equals method is greatly reduced, almost only once or twice.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325296535&siteId=291194637