[JAVA Basics] Hashcode () & Equals ()

Source: Internet
Author: User

The use of map is usually a Java native type, so there is little focus on the internal implementation of the methods of Hashcode () and Equals (). The recent realization of a small tool, involving their own writing of the class lookup, and again revisit the relevant knowledge.

On the simple example code, the difference between the overwrite and the non-overwrite

Do not overwrite hashcode () and Equals ():

1 ImportJava.util.HashMap;2 ImportJava.util.Map;3 4  Public classHashcode_equals {5 6      Public intA;7      Public intb;8 9      Public Static voidMain (string[] args) {Ten  OneMapNewHashmap(); A  -Hashcode_equals obj1 =Newhashcode_equals (); -obj1.a = 1; theobj1.b = 1; -Map.put (obj1, "test"); -  -Hashcode_equals Obj2 =Newhashcode_equals (); +obj2.a = 1; -obj2.b = 1; +  ASystem.out.println ("obj1.equals (OBJ2) =" +obj1.equals (OBJ2)); atSystem.out.println ("map.get (OBJ2) =" +Map.get (OBJ2)); -     } -}
View Code

Execution Result:

Obj1.equals (OBJ2) =false
Map.get (OBJ2) =null

Overwrite only equals () method

1 ImportJava.util.HashMap;2 ImportJava.util.Map;3 4  Public classHashcode_equals {5 6      Public intA;7      Public intb;8 9 @OverrideTen      Public Booleanequals (Object obj) { One         if( This==obj) A             return true; -         if(obj = =NULL) -             return false; the         if(GetClass ()! =Obj.getclass ()) -             return false; -Hashcode_equals other =(hashcode_equals) obj; -         if(A! =other.a) +             return false; -         if(b! =other.b) +             return false; A         return true; at     } -  -      Public Static voidMain (string[] args) { -  -MapNewHashmap(); -  inHashcode_equals obj1 =Newhashcode_equals (); -obj1.a = 1; toobj1.b = 1; +Map.put (obj1, "test"); -  theHashcode_equals Obj2 =Newhashcode_equals (); *obj2.a = 1; $obj2.b = 1;Panax Notoginseng  -System.out.println ("obj1.equals (OBJ2) =" +obj1.equals (OBJ2)); theSystem.out.println ("map.get (OBJ2) =" +Map.get (OBJ2)); +     } A  the}
View Code

Execution Result:

Obj1.equals (OBJ2) =true
Map.get (OBJ2) =null

overwrite both hashcode () and Equals ()(automatically generated using Eclipse-integrated features)

1 ImportJava.util.HashMap;2 ImportJava.util.Map;3 4  Public classHashcode_equals {5 6      Public intA;7      Public intb;8 9 @OverrideTen      Public inthashcode () { One         Final intPrime = 31; A         intresult = 1; -result = Prime * result +A; -result = Prime * result +b; the         returnresult; -     } -  - @Override +      Public Booleanequals (Object obj) { -         if( This==obj) +             return true; A         if(obj = =NULL) at             return false; -         if(GetClass ()! =Obj.getclass ()) -             return false; -Hashcode_equals other =(hashcode_equals) obj; -         if(A! =other.a) -             return false; in         if(b! =other.b) -             return false; to         return true; +     } -  the      Public Static voidMain (string[] args) { *  $MapNewHashmap();Panax Notoginseng  -Hashcode_equals obj1 =Newhashcode_equals (); theobj1.a = 1; +obj1.b = 1; AMap.put (obj1, "test"); the  +Hashcode_equals Obj2 =Newhashcode_equals (); -obj2.a = 1; $obj2.b = 1; $  -System.out.println ("obj1.equals (OBJ2) =" +obj1.equals (OBJ2)); -System.out.println ("map.get (OBJ2) =" +Map.get (OBJ2)); the     } - Wuyi}
View Code

Execution Result:

Obj1.equals (OBJ2) =true
Map.get (OBJ2) =test

The Equals () method is no longer redundant, and the focus is on how hashcode () is better implemented

"Effective Java" 9th: Overwrite equals when always overwrite Hashcode

Specification requirements: Objects of equal size must have the same hash code. Otherwise the hash code obtained from the map as the key value is different, so the value of the result is different, the above example is a good proof

In the case of no overwrite, the Hashcode method is generated by default in Java. From the code execution results see Obj1.equals (OBJ2) is true, still hashcode different, also said Java default is not meet the requirements, we must overwrite.

With regard to hashcode, if the method is simple to write, it will cause hashcode to conflict, so that each object is mapped to the same hash bucket, making the hash table degenerate into a linked list. Makes the program that should be linear time run into a square-level operation.

So effecive Java also gives a reference to the implementation

[JAVA Basics] Hashcode () & 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.