JAVA equals, = =

Source: Internet
Author: User

are judged equal, no difference to basic variables, except for dynamic variables (that is, objects):

    1. = =: 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.
    2. 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 ()

JAVA equals, = =

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.