Home > Back-end >  Hash 'Set the equals and hashCode methods
Hash 'Set the equals and hashCode methods

Time:12-04

A little don't understand the meaning of these two methods, especially the hashcode method inside some of the parameters, bosses more, please,,,


CodePudding user response:

Hashcode methods is on the basis of the original 31
Rewrite the equals method is mainly based on the title whether book two objects are equal

CodePudding user response:

HashCode function, it is a hash function, or characteristic function,
Use an int the characteristics of the type of data to represent the entire object (characteristic), and asked the int type data, as far as possible to return different values for different objects (dispersion characteristics),

The sample code of the building Lord, is not typical, at the very least, without considering the money attribute, of course, some application scenarios ignore certain attributes are understandably,
As to why there are 31, personal feel there is no use, see the meaning of the code should be want to let algorithm has more decentralized nature, however, it seems to reach this goal,

Don't understand hashCode and equals two functions?
Recommend the building Lord see HashMap data structure (pay special attention to data storage and read process),
Simply to say, hashCode function can accelerate the positioning (or find) belongs to the speed of the object; Equals the function can be precise positioning (or find) belongs to object exists,

HashSet object inside will never repeat, isn't it?
So, in the HashSet into the object, is to see if there has existed the same objects?
How to determine whether already exists the same object, is get to into the inside of the object and HashSet all objects are compared,
What does hashCode in the process? Is through the value of the int can ignore some don't have to compare the objects, accelerate the search process,
Equals what role in this process? Is the complete object comparison process, finally will be subject to the value returned from this function,
  • Related