Determine if the elements in the set are duplicated, = =, equals, Hashcode method research-code Demo

Source: Internet
Author: User

The class was tested without overriding the Hascode () and Equals () methods:

 Package Niukewang; Import java.util.Objects;  Public class SetClass {    String A;    String b;      Public SetClass (String A, string b)    {        this. a=A;          this. b=b;        } }


Test class:

 PackageNiukewang;ImportJava.util.HashSet;ImportJava.util.Set; Public classTest1 { Public Static voidMain (String args[]) {setclass S1=NewSetClass ("http://www.yjbys.com/", "" "); SetClass S2=NewSetClass ("http://www.yjbys.com/", "" "); SetClass S3=NewSetClass ("http://www.yjbys.com/", "" "); Set<setClass> set=NewHashset<>();        Set.add (S1);        Set.add (S2);                Set.add (S3); String SS=NewString ("Hello"); String SS1=NewString ("Hello"); SYSTEM.OUT.PRINTLN (SS==SS1);        System.out.println (Ss.equals (SS1)); System.out.println (Ss.hashcode ()==Ss1.hashcode ()); System.out.println ("Set ...."); System.out.println (S1==S2);        System.out.println (s1.equals (S2)); System.out.println (S1.hashcode ()==S2.hashcode ()); System.out.println ("Number is" +set.size ()); }}

Output Result:
False (string is not the same object)
True (as long as the values are the same)
True (Hashcode is the same)
Set ....
False (not the same object)
False
False
Number is 2

This is the case where the hashcode () and Equals () methods are not covered.

The tested class overrides the Hashcode () and Equals () methods:

 PackageNiukewang;Importjava.util.Objects; Public classSetClass {String A;    String b;  PublicSetClass (String A, string b) { This. a=A;  This. b=b; }         Public inthashcode () {returnA.hashcode (); }         Public Booleanequals (Object obj) {if(obj==NULL)return false; if(GetClass ()! = Obj.getclass ())return false; FinalSetClass s=(setclass) obj; returnObjects.equals ( This. A, S.A.); }}

After overwriting, the value of Hascode is the value of the hashcode,equals () of the set of A and the hashcode of a.
Test result output:

False
True
True
Set ....
False
True
True
Number is 1

The internal implementation of the set is actually a map, the Hashcode method is called when processing the key of the map, and the code in the HashMap is as follows

int hash (Object key) {        intnull)? 0: (H = key.hashcode ()) ^ (h >>>);}   

Debug Proof: Add element to set, first will compare hashcode is equal, if the hashcode is not equal to add this element directly to set, if the hashcode equals the Equals method, if equals is not equal to add this element to the set, So the element repeatability of set is judged by the hashcode and the Equals method,

The reason why overriding the Equals method must overwrite the Hashcode method is also shown: Because the Hashcode method is called first, if the Hashcode method is not overwritten, the physical address of the memory is taken as the basis for generating hashcode by default. Then the hashcode of two different objects must be different, so the direct end of the add, simply cannot call to the Equals method, it is not to mention the internal implementation of equals, regardless of whether the equals is returned true or false is not a chance to call.

Because the set is implemented inside a map, it is the same principle to put elements in a map.

Proving that if the Hashcode method is not overridden, it is useless whether or not the equals return True or FALSE, because the Hashcode method is called before the Equals method is called, and when the Hashcode method is called

It is considered that these objects are all non-repeating elements, adding these objects directly to the set, and completing the addition, the Equals method has no chance of being called.

Learn from Blog: http://www.cnblogs.com/langtianya/p/4421582.html

Determine if the elements in the set are duplicated, = =, equals, Hashcode method research-code Demo

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.