Software configuration notes --3.5 Equality in ADT and OOP

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/lll_90/article/details/89264286

First, the equivalence ADT is defined mainly in three ways:
1.AF : R → A a same data structure mapping abstract values,
i.e., the AF (A) = the AF (b)
2.a and b is equivalent to, if a and b in the same equivalence class (a and b satisfy a reflexive, symmetrical, transfer relationship)
3. any operation on the two objects are the same as the results obtained from the viewing angle.
Second, with ==. Equals () difference
Compare that memory address;
equals () compares the contents of the object of
tips: In the not override the equals () object defaults in the parent class inherits equals the Object () method, the same effect
.
Thus, when necessary rewriting class equals () and hashCode (). However, it must be noted that the method signature must be consistent with the parent class method is overridden.
For example:
Here the parent class method different parameter list, so to achieve the override effectOutput false
modified as follows better:
Here Insert Picture Descriptionoutput true.
Note: In addition to implement equals () method, and avoid using the instanceof getClass () running
type (Dynamic Check) detection object, when there are security risks,
another example, an equivalent inconsistency occurred here.
Here Insert Picture DescriptionExportThird, the required rewriting the override
1. satisfy equivalence (reflexive, symmetrical, transfer)
2. consistency, when the information used in the comparison has not been modified, the results should always be the same for multiple comparisons
3. Use the method of determining equals two equal objects, which
hashCode must produce the same results.
4. null handling: comparing null, x must return false eg a reference for any non-empty, x.equals (null) return to false...
For trans Li:
The above code does not satisfy the transitive equivalence relation.
When a = 5, b = 0, c = -5 , the equivalence relation has a and b, b and C, but not equivalent to a and b. Seen, this does not satisfy the equivalence relation is transitive.
Four, HashCode principle described
below, the key value pairs are mapped to hashCode, corresponds to the index of the array, hashCode determines that the location data is stored in the array. When multiple key hashes to the same index when the (conflict), the hash table to maintain a list to record these key-value pairs (bucket)
Here Insert Picture DescriptionQuery process: using hashCode () produced is determined hashcode slot (index), then equals () method to find a matching key in the bucket.

Five, hashCode () rewrite requirements
1. As long as the information used in the comparison operation has not been modified, then this same object called many times, hashCode () method must always return the same integer. (The same as when the program does not require multiple execution) unless the object variable, otherwise hashcode can not be modified.
2. If equals relatively equal, hashcode with the requirements
and so on; equals, if not equal, the hashcode for equality can be, but the best range (to improve performance)
3. Because most languages are based on object memory address as the default hashcode, when considered object configured hashcode all fields, in order to avoid unequal objects produce the same hashcode
Java provides Objects.hash () method, which generates a plurality of fields hashCode ()
six, the variable type equivalence interpretation
equivalence variable divided into two types:
1. equivalent observed: in the case where the object does not change the state (without using mutator), the object can not be distinguished
emm, can be understood as the result obtained by various methods observer is the same
2. equivalent behavior: to change an object without changing another time, still can not distinguish objects
emm, that is, two references point to the same object
for variable type using the observation equivalence would bring some bug.
For chestnut following:
Here Insert Picture DescriptionExportthe Collections equivalent implementation is observed, when the same collection of elements, the two are equivalent, but when the element is changed within the set, the equivalence inconsistency arises, as follows:
Here Insert Picture DescriptionExportas the picture showsThe reason is that the above-mentioned problems hashCode will change the content changes, the index references to the corresponding bucket has changed; but on the Bucket of HashSet has not changed, but the store in the original position hashSet lost. Thus breaking the rep invariance.
Therefore, the variable type should Equals () should achieve equivalent behavior, the direct successor equals Object class () and hashCode ().
Seven, and finally the equivalence mechanism under Autoboxing
Here Insert Picture Description
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/lll_90/article/details/89264286