Home > Back-end >  Thinking about hashcode cause wish you glad
Thinking about hashcode cause wish you glad

Time:10-01

Say my thinking (stayed up for debate and bad to verify, please advise) : first of all, we should understand a sequential order problem, I think, is after instantiating an object to be new, in the address, and then to calculate its hashcode, use this hashcode just as the object in the Java a logo, that is to say, as long as the new one can produce an object (sounds like bullshit, but detail), that is to say, no matter the value of the object are equal, or not is different objects, and then the==(the symbol), is the address of the two objects, the objects inside the JVM heap address (I think it is a numerical directly address is the starting address of the computer to store the object memory), we generally is the object with a reference variable references, for example the User User=new User (" ghost valley ", "22"); Among them, the user's reference variables and store it into the stack of the JVM, and new user (" ghost valley ", "22") this is a new object, on the heap, stack is logically, and heap is putting things, here is the new user (" ghost valley ", "22") the address of the value to the user, for the user to the new user (" ghost valley ", "22"), I think the address of the user itself is let the operating system process, through the elastic stack to run the program, when the==comparison, is by comparing tell a reference to judge whether the value of the object are equal, then the value of the reference object is his only points to the starting address of the object, if it is a point to the same object values are equal, and vice is not equal to, a, for the following, I think, as long as it is, I think is not sure, I hope you sure is it clear) for basic data types, if it is in the method, on the stack, when running programs that take the box (feeling that's why the method cannot be defined in the scope of reason), and in the basic data types in a class, as properties, should be a new come out later in a heap, wait until run time from the pile and show the method if the reference to the object, then the method of attribute values assigned to the definition of a variable, such as method: int I=user. GetAge (); User. GetAge () is from the inside of the heap to the value, and I are in the stack, if is a direct comparison, such as user getAge ()==user1. GetAge (), is to take out two values, on the stack, comparing the size of the two basic variable values, namely, compare their binary code, do a little, I think in the stack, basic types and the reference object is a binary code, forms, basic data type is of value, such as int I=1; Is on behalf of the stack of saved 1 this value (the position in the stack is the binary code and the top is 0, after four or 0001), and a reference type, is stored in the heap object's address, and binary code==is to compare the two Objects are the same, namely it is the starting address of the heap object comparison, added hashcode, before the hashcode not be rewritten, is according to the address of the object inside the JVM heap, a numerical calculation, the address used to identify the object in Java (I think this may not be unique, different object calculated hashcode could be the same), and rewrite, such as the return Objects. The hash (id, name, munber); This is according to the new attributes of the object of value, to calculate hashcode, many people think the values and comparing the equals size are inseparable, but I think the two are no relationship, whether it is equal to rewrite before or rewritten, from beginning to end have never mentioned hashcode (maybe I didn't find, if found, which would you please let us know), after override hashcode (the default override method, all properties are to be included in the hash parameters) is to judge whether the properties of the two objects,
Such as different two kind of new properties of the same object, if the property value and hashcode way of rewriting the same way, the same hashcode, what also doesn't explain, at most two objects must be unequal, finally is equals to the and==there is comparative, not rewritten as return before (this==obj); Two effects are the same, but rewritten as follows:
Equals rewritten, first of all determine whether the two reference object reference address, if the same is cited the inside the same object (because of the==comparison real address), the same object, there is no question that must be the same; Otherwise, then run the method, according to the reflection determine whether from the same Class Class, avoids the similar attributes and attribute values same problem, this is the place better than hashcode, such as the above examples of hashcode, if not also showed the same hashcode an object, and then, if the same, as are compared, and the following basic types using the==comparison, in the base type, if the same numerical returns true, it has been done in this paper, the basic types of comparing equals, but ultimately did use the==comparison, without any relationship of hashcode, so I just wondering why rewrite equals to rewrite the hashcode?
Above is my to==, hashcode, equals the thick thin see, have wrong place entreated you pointed out,

CodePudding user response:

Basically:
HashCode is mainly used to improve the query efficiency of improving the performance of hash table to determine the object stored in the hash structure address
1, override the equals () must override hashCode ()
2, the hash in the storage structure, add elements repetitive calibration standard is first check hashCode values, after judging the equals ()
Equal 3, two objects the equals () and hashcode () must be equal to
4, two objects hashcode () and equals () must also differ
5, two objects hashcode () are equal, object is not necessarily equal, need through the equals () further judgment,

Rewrite equals after must rewrite hashcode purpose is to ensure that equals the same objects in the hashmap/hashset the same hash bucket, so as to improve the retrieval speed,







CodePudding user response:

I think the original poster is misunderstood hashCode usage, an object's hashCode is to determine the uniqueness of the object, is the perfect solution for each instantiation of the object has a completely unique hash code as the uniqueness of his label, but the "uniqueness" are difficult to achieve, so there was a compromise agreement: if two object instances are the same a.e quals==true that a and b (b) the hash code must be the same, but if a.e quals (b)==false, their hashCode may also be the same,

Hash Code used mainly in the Hash by looking for key of the implementation of data structure, the corresponding values, the aim is to search and return the target object instance, one of the most commonly used is a hashmap. Can you imagine a hashmap as a row of barrels, each barrel label represents a Hash value, if according to the most perfect condition, each Hash Code value is unique, then each bucket, there is only one instance will very quickly find, but the reality could be more elements, each bucket when find the barrels, system also need according to the equals method from the multiple elements to find corresponding instance,

Java provides the Comparator/Comparable interface to implementation instance comparison

CodePudding user response:


"Why rewrite equals to rewrite the hashcode"
Because there is an important part of the specification: equals return true two objects, their hashcode must
So if you just override the equals, hashcode probably return value is not the same as
So, in the ArrayList class array type seems to don't see any problems in the collection

But if it is a HashMap key, so the question is exposed in the
This is not just the problem of retrieval speed
But you probably cannot do you expect from a HashMap the element

You look at the HashMap get source:
 
Public synchronized V get (Object key) {
Entry TAB []=table;
Int hash=key. HashCode ();
Int index=(hash & amp; 0 x7fffffff) % TAB. The length;
For (Entry E=TAB [index]; E!=null; E=e.n ext) {
If ((e.h ash==hash) & amp; & E.k ey. Equals (key)) {
Return e.v alue.
}
}
return null;
}

Focus on this sentence: if ((e.h ash==hash) & amp; & E.k ey. Equals (key)) {
If your object is as HashMap key
Is first to determine hashcode

HashSet also have the same problem, first determine hashcode
If the hashcode is different, have already considered the different objects,
Is your equals two object to true if you don't like hashcode, exists in the Set will repeat, contrary to the meaning of the Set
  • Related