The use of hashCode method

  • 2020-04-01 01:34:20
  • OfStack

First, to understand what hashCode does, you have to know about collections in Java.
In general, there are two types of collections in Java, a List and a Set.
Do you know the difference? The elements in the former set are ordered and can be repeated. The latter element is unordered, but the element is not repeatable.
So here's the big question: what does it take to make sure that two elements are not duplicated?
So that's the object.equals method. However, if you check every element you add, then when there are a lot of elements, the number of times that elements are added to the collection will be very large.
That is, if there are already 1000 elements in the collection, it will call equals 1000 times when the 1001st element is added to the collection. This obviously reduces efficiency considerably.      
Therefore, Java adopts the principle of hash table. A Hash is actually a person's name, named after him because he came up with the idea of a Hash algorithm.
Hash algorithms, also known as hash algorithms, assign data to an address directly according to a particular algorithm. If I were to go into the details of hashing, it would take a lot more writing, and I won't go into it here.
For starters, the hashCode method actually returns the physical address of the object's storage (which may not be).    
This way, when the collection adds a new element, the hashCode method for that element is called first, and all of a sudden it is positioned where it should be physically.
If there are no elements in this location, it can be stored in this location without any comparison. If I already have an element here,
Call its equals method and compare it with the new element. If it is the same, it will not save it. If it is different, it will hash other addresses.
So there's a conflict resolution issue here. This reduces the number of actual calls to equals to almost one or two.    
So, the eqauls and hashCode methods are specified in Java as follows:
1. If two objects are the same, their hashCode values must be the same; 2. If two objects have the same hashCode, they are not necessarily the same         The above reference to the same object refers to the comparison using the eqauls method.    
Of course you can go against the rules, but you'll find that the same object can appear in the Set Set. At the same time, the efficiency of adding new elements is greatly reduced. Hashcode is a method used to determine whether two objects are equal.
So you say, isn't there an equals method? Yes, both methods are used to determine whether two objects are equal. But there is a difference. In general, the equals method is called for the user, and if you want to determine whether two objects are equal,
You can override the equals method and call it in your code to determine if they are equal. In simple terms, the equals method is primarily used to determine whether two objects are equal, either visually or in terms of content. For example, there's a student class,
Properties only have name and gender, so we can assume that as long as the name and gender are equal, then the two objects are equal. The hashcode method is usually not called by the user, for example, in a hashmap, because the key is not repeatable,
He determines the method hashcode when he determines whether the key is repeated, and he also USES the equals method. You can't repeat equals and hashcode as long as you have one not equal! So in a nutshell, the hashcode phase
  When an object is encoded like md5 in a file, it differs from equals in that it returns an int, which is not intuitive. We usually override hashcode as well as equals to make their logic consistent. For example,
Again, if the name and gender are equal even if the two objects are equal, then the method of hashcode also returns the hashcode value of the name plus the hashcode value of the gender, so that logically they are the same.
To physically determine whether two objects are equal, use ==


Related articles: