The difference between hashcode¥equals¥== in Java

Source: Internet
Author: User

Ref:http://www.cnblogs.com/skywang12345/p/3324958.html

1. = = function:

the = = in Java is used to determine whether the address of two objects is equal, and when the object is a basic data type, it can be used to determine whether the value is equal;

2, Equals ():

The Equals () method defined in the object class, return (this = = obj) , and by = =, many Java classes override the Equals method, Java's rewriting of the Equals method requires that the following conditions be met:

1. Symmetry: if X.equals (y) returns "true", then Y.equals (x) should also return "true". 2. Reflectivity: X.equals (x) must return is "true". 3. Analogy: If X.equals (y) returns "true" and Y.equals (z) returns "true", then Z.equals (x) should also return "true". 4. Consistency: if X.equals (y) returns "true", as long as the X and Y contents remain constant, the return is "true" no matter how many times you repeat X.equals (y). 5. Non-nullability, x.equals (null), always returns "false"; X.equals (and X objects of different types) will always return "false".

As follows:

@Overridepublic boolean equals (Object obj) {          if (obj = = null) {              return false;          }                        If the same object returns True, the inverse returns false          if (this = = obj) {              return true;          }                        Returns the run-time class of the object, judging whether the type is the same.        if (this.getclass () = Obj.getclass ()) {              return false;          }                        Person person = (person) obj;          Return Name.equals (person.name) && age==person.age;  The Equals method of the string class was called}     
3, Hashcode ():

1, when using the hash list, the Hashcode () method needs to rewrite and play a role!

Hashcode (): Gets the hash code that returns an integer of type int. When you need to put this class into a hash list (such as HashMap), you need to use the Hashcode method to determine the storage address, in the object already exists in that address with equals to determine whether the value is equal. If you skip hashcode judgment, it is possible that the O1 object is the same as the O2 value (attribute value) of the object being stored, and O2 should be replaced with O1, but because O1.hashcode ()!=o2.hashcode (), ( The Hahscode method is called, and the hash list does not have an element at the address of the hash code, then the O1 is placed in the address. This resulted in the existence of two identical objects in the HashMap, violating the design principles of HashMap.

2. In a hash list such as Hashmap,hashtable,hashset, the override of the Equals () method requires an override of the Hashcode () method, following:

    

(1): If O1.equals (O2) ==true, then must have O1.hashcode () ==o2.hashcode (), and vice versa; (2): If O1.equals (O2)! =true, the hashcode can be equal or unequal;

  

The above is purely personal understanding!

    

       

  

The difference between hashcode¥equals¥== in Java

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.