|
are judged equal, no difference to basic variables, except for dynamic variables (that is, objects):
- = =: reference equality (reference comparison). For object references, that is, the reference value is equal to the address. That is, if object a A, a==b indicates whether the addresses of a and b are equal.
- Equals (): reference points to equal content (value comparison). Is the method of object, and its implementation is return this = = Inputobject. However, this method is often overridden, such as String,integer, to determine whether the object reference is equal to the content
- A==b A.equas (b) must be correct, but the other is not necessarily, to see object A for the specific implementation of equals. If the default object implementation is used, then vice versa.
- There are specifications, if A.equals (b), then a.hashcode () = = B.hashcode (), or vice versa, that is, if hashcode () is equal, a.equals (b) is not necessarily correct, because there may be conflicting problems with hashcode. Therefore, if overriding the Equals method is overridden, be careful to modify the Hashcode () method to satisfy the specification.
Understanding the implementation of common data structures for equals is also necessary for dealing with practical problems:
- Object: The object () method is defined, and the implementation is return this = = Inputobject. For other objects, the implementation of object defines the requirements: symmetry, self-reflection, transitivity, consistency, and space. See one.
- Collection: There are no other special requirements for the Equals () method. That is, if a class implements Collection, it is generally not recommended to overwrite the Equals method of object unless there is a special need;
- list,Set: In list, it is required that both A and B are list,size equal and contain the same elements in the same position. (The same element here takes the Equals method of the element). Set because there is no order, it is not required to be in the same position, requiring only one set of all elements to be in the other set. See figure four for the third and three.
- Abstractmap: Two maps are equal, judging that two map contains the same mappings. Mapping equality determines whether key-value pairs are equal: key is equal to the ContainsKey method, which makes such judgments:(key==null? K==null:key.equals (k)) .. For values equal, value.equals (m.get (key)) is used. that is, the key-value pair is equal to the Equals method that takes the key value object itself. See figure Five.
- HashMap: Inherits the Equals method of Abstractmap. In particular, it is used to determine whether a key value exists in its Put method:(k = p.key) = = Key | | (Key! = null && key.equals (k))).
Figure one: Object.Equals ()
Figure two: Collection.equals ()
Figure three: List.equals ()
Figure Four: Set.equals ()
Figure V: abstractmap.equals ()
|