2013年9月20日星期五

Two objects the same value (x.equals (y) == true), but it may have a different hash code, this sentence right ?

Two objects the same value (x.equals (y) == true), but it may have a different hash code, this sentence right ?
Seeking to explain
------ Solution ---------------------------------- ----------
1. the same application , the function of an object equals hashCode function does not change the situation , regardless of how many times called , it must return the same integer.
2. calls equals function if two objects are equal , then calling the hashCode function will return the same integer .
3. calls equals function if two objects are not equal , then calling the hashCode function does not require a certain return distinct integers
------ Solution --------- -----------------------------------
this sentence is false.

two different object instances , hash code can be equal, because there is no perfect hash function .
On the contrary , it is wrong , because you can not guarantee that users of the class does not use HashSet / HashMap
------ Solution --------------- -----------------------------

last sentence the wrong object hashCode is the only marker ; only if the two objects are equal to their equals results mark must be equal

http://blog.csdn.net/KindAzrael/article/details/4633615
------ Solution ---------------------------------------- ----
directly on the API




public boolean equals (Object obj) indicate whether some other object associated with this object " equal ."
equals method is implemented on non-null object reference equivalence relation :

reflexive : for any non-null reference values ​​x, x.equals (x) should return true.
Symmetry: For any non- null reference values ​​x and y, if and only if y.equals (x) returns true , x.equals (y) ; only should return true.
transitive : for any non -null reference values ​​x, y and z, if x.equals (y) returns true, and y.equals (z) ; returns true, then x.equals (z) should return true.
consistent: for any non- null reference values ​​x and y, multiple invocations x.equals (y) consistently return true or consistently return false, provided that objects used in equals comparisons on the information has not been modified .
For any non- null reference values ​​x, x.equals (null) should return false.
Object class equals method on the object most likely equal to the difference between ; That is, for any non- null reference values ​​x and y, if and only if x and y refer to the same object, this method returns true (x == y has the value true).

Note: When this method is overridden , it is usually necessary to override the hashCode method , in order to maintain the general contract of hashCode method , which states that equal objects must have equal hash codes .


parameters:
obj - the reference object with which to compare .
Returns:
If this object is the same as the obj argument , returns true; otherwise false.


-----------------

public int hashCode () Returns the hash code value for the object . This method is supported for the hash table to provide some advantages, such as , java.util.Hashtable hash table provided .
hashCode general agreement that:

during the execution of a Java application , on the same object multiple times calling the hashCode method must consistently return the same integer , provided that equals comparisons on the object information is not used is modified. From one execution of an application to another of the same application execution , the integer need not remain consistent .
if under equals (Object) method , two objects are equal , then the two objects calling the hashCode method on each object must be produce the same integer result .
The following are not be necessary: ​​if according to equals (java.lang.Object) method , two objects are not equal, then the two objects on any object in calling the hashCode ; method must produce distinct integer results. However, the programmer should be aware that the unequal objects produce distinct integer results can improve the performance of the hash table .
In fact, the hashCode method defined by class Object does return different objects for different integer . ( This usually is done by converting the internal address of the object into an integer to be achieved, but does not require such JavaTM programming language implementation technique . )


Returns:
this object a hash code value .

------ For reference only ---------------------------------- -----
same value using the equals method to determine , equals method after rewriting is not necessarily determine whether the object is the same object
So different is normal hashcode
like I defined a class , has a name and number
I override the equals method , so that the same name is returned equal
So although I have hashcode two different objects ( their numbers may be different or the value of all the properties of two objects are the same )
but they certainly different hashcode
because there is not an object Well
------ For reference only --------------------------- ------------
this sentence is correct

equal objects may not be the same hashcode
In turn , hashcode is not necessarily the same as equal
------ For reference only -------------------------- -------------
right


...

------ For reference only --------------- ------------------------
but generally override equals to override both HashCode
------ For reference only ---------------------------------------
this sentence is correct .
equals and hashCode are two methods that can be overridden .
So the key to how you override these two methods .
------ For reference only -------------------------------------- -
2nd floor theory is right, but in accordance with normal business logic , unless it is neuropathy or else override equals and hashCode methods is certain as true equals , hashCode equal
--- --- For reference only ---------------------------------------


hashcode is not the only mark if the judge depend on it to carry out equal , naturally, must be equal , if not rely on it to make equal judgment, it is what is not is
------ For reference only --- ------------------------------------
is right Mile !
------ For reference only -------------------------------------- -
However , API stated that only one agreement , and did not force you to have to do it
------ For reference only -------------- -------------------------

hash conflict is likely to do
------ For reference only - --------------------------------------
two objects are equal , hashcode certainly consistent ...
------ For reference only -------------------------------------- -
access points friends.
------ For reference only -------------------------------------- -
answer equals (2 F ) , but not in my reply , 2nd Floor , -----> hashCode () can of course different
---- - For reference only ---------------------------------------

last sentence the wrong object hashCode is the only mark only if the two objects are equal to their equals results mark must be equal

http://blog.csdn.net/KindAzrael/article/details/4633615

System.out.println ("Aa". hashCode ());
System.out.println ("BB". hashCode ());
------ For reference only ------------------ ---------------------
wrong, have the same hash code.
------ For reference only ------------------------------------ ---

last sentence the wrong object hashCode is the only mark only if the two objects are equal to their equals results mark must be equal

http://blog.csdn.net/KindAzrael/article/details/4633615


Why is it wrong, you did not say that there has not been rewritten equal , rewritten hashCode is not necessarily higher than
------ For reference only ------ ---------------------------------
landlord, I tested for the Object class must have equal equals hashCode equal. Look API is easy to understand .
For the String class , the same conclusion holds.
For other custom classes , this conclusion is uncertain, equals and hashCode depends on the specific implementation.

没有评论:

发表评论