Why would Java rewrite the Hashcode method and the Equals method?

Source: Internet
Author: User

Previously published an article about the Equals method rewrite http://www.cnblogs.com/aL0n4k/p/4777333.html

Below on the Hashcode method to publish my understanding, for reference only, Exchange.

In relation to the Java rewrite Equals method, it has been mentioned that when comparing 2 objects, compare their respective properties.

So the overriding Hashcode method is because when we touch the collection, there is a set interface, and he can only add unordered and non-repeating object elements.

Then some people will ask, since it is so that we directly use equals to judge not to be finished?

In fact, for set sets, he needs to compare the Hashcode value of an object, that is, the reference address of an object (the storage address in memory)

Set is called Hashcode to determine whether the value of the address is the same, if not the same is inserted directly, the same way call the Equals method to determine whether the content is the same, if the same is not inserted, not the same is inserted.

Overriding the Hashcode method is to make a specific element comparison with those elements that have been inserted (this is intentionally to have two object reference values the same, just to determine whether 2 object properties are the same)

1. When the address values of two objects are different: then even if their attributes are the same content, they are not judged by equals and are not inserted directly into the collection.

2. Two object address values compared to the same time: then to compare with equals, then equals is finished or the same, do not insert. Insert it differently.

 

Summary:

So let's say we want to override the Hashcode method in cases where 2 objects (memory values are not the same), but the property content is the same as inserting only one of the two

Overriding the Hashcode method is to get the Equals method to compare the properties of the object, because the JDK (my 1.8) default provides a Hashcode method that simply returns 2

The code value of the object, and because we are referring to the object created by new, it must be a different address, so we need to rewrite the address value to have all 2 objects return the same. Then the Equals method is more specific.

Property, then decide whether you want to insert this element.

Of course, faster is to speed up the insertion of the retrieval, because the next need to call the Equals method to judge. If the value of their address is judged directly by hashcode, then it is not necessary to judge the Equals method.

When set is inserted, you can see http://blog.csdn.net/liushuai_ly/article/details/8197508

1 ImportJava.util.HashSet;2 ImportJava.util.Set;3 4  Public classTest_set {5 6      Public Static voidMain (string[] args) {7person P1 =NewPerson ("xiaoming", 11);8         //Suppose P1 is you .9person P2 =NewPerson ("xiaoming", 11);Ten         //Suppose P2 is your classmate, he is also known as Xiao Ming, then he is as old as you are 11 years of age. One         //so we're going to assume he's the same person? A          -         //This is 2 people, right? (But they are 2 people compared to hashcode, but we just need to insert an object because they are the same property) -         //then we need to rewrite the Hashcode method and the Equals method the         //P1 and P2 are all object references created through new, and their memory values (hashcode code values) are not the same -         //so even though their object attributes (age) are the same, they are considered to be 2 people. -         //then we need to rewrite the Hashcode method so that the value of the Hashcode code returned by the two objects is the same. -         //That's when we're going to use the Equals method to compare the attributes between them, if the comparison is done. +         //then it means that they are an object, so we do not insert the latter element into the set set. -         //if not, we will insert the P2 object. +         //But if you say that I don't have to, I'm going to put 2 objects of all the same attributes into the collection, so you don't have to rewrite those things. A         //without rewriting, they are always different 2 objects when comparing hashcode values (even if all of their properties are identical) at          -Set<person> set =NewHashset<person>(); - Set.add (p1); - Set.add (p2); -         //output all the set elements and see if you can output two objects. - System.out.println (set); in         //I machinethe output is [[email protected]] -  to     } +      - } the classPerson { * String name; $     intAge ;Panax Notoginseng      -      the      PublicPerson (String name,intAge ) { +          This. Name =name; A          This. Age =Age ; the     } +      -  $     /*@Override//rewrite hashcode $ public int Hashcode () { - return age + Name.hashcode (); - //Return a fixed value, if the same as the value of the address, then to the Equals () judgment, not the same, directly inserted. the     }*/ -     Wuyi      the      - @Override Wu     //Eclipse Auto-generated hashcode overrides -      Public inthashcode () { About         Final intPrime = 31; $         intresult = 1; -result = Prime * result +Age ; -result = Prime * result + ((name = =NULL) ? 0: Name.hashcode ()); -         returnresult; A     } +  the  - @Override $      Public Booleanequals (Object obj) { the         if( This==obj) the             return true; the         if(obj = =NULL) the             return false; -         if(GetClass ()! =Obj.getclass ()) in             return false; theperson other =(person) obj; the         if(Age! =other.age) About             return false; the         if(Name = =NULL) { the             if(Other.name! =NULL) the                 return false; +}Else if(!name.equals (other.name)) -             return false; the         return true;Bayi     } the      the  -}

Why would Java rewrite the Hashcode method and the Equals method?

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.