Why rewrite the equals method generally have overridden hashCode method

Now suppose we have a Rectangle class Rectangle 

class of the Rectangle {public
Private int length;
Private int width;

public of the Rectangle (int length, int width) {
this.length = length;
this.width = width;
}
}
at first we review why should override equals methods? Now we have two rectangular objects and instances of classes rectangle1 rectangle2.

= New new rectangle1 the Rectangle the Rectangle (. 3,. 4);
the Rectangle = new new rectangle2 the Rectangle (. 4,. 3);
I want to know if the two rectangles are equal, if == operator, then the comparison and reference rectangle1 of rectangle2 reference point to the position of the object stored in the heap. Has two new objects are out, so it must be in the heap there are two different objects, the result is bound to be false.

System.out.println (rectangle1 == rectangle2);
output: false
This is not what we want, our purpose is not to want to know is whether these two references point to the same object, but would like to know two rectangles the length and width are the same, in short, what we want is to know that through logic judgment and rectangle2 rectangle1 two rectangles are equal. So we achieved by covering the equals method.

@Override
public Boolean the equals (Object obj) {
the Rectangle = O (the Rectangle) obj;
IF (length == o.length) {
IF (width == o.width) {
return to true;
} return the else to false;
} the else IF ( == o.length width) {
IF (length == o.width) {
return to true;
} return the else to false;
}
return to false;
}
this equals methods, we developed two rectangular rule, have the same length, width, i.e. that two rectangles are the same.

Then again judged by whether the two rectangles are equal equals method

System.out.println (rectangle1.equals (rectangle2));
output: true

Here we have to determine whether we meet the same needs of two rectangles.

Why should we cover hashCode methods?

If the required class is put in the rectangle based on a hash table to achieve similar HashMap container or the like and as a key, it is necessary to cover hashCode method, according to our custom rules to obtain the hash value of a rectangle. (If not specified, the default is usually the object by converting the internal address into an integer achieved)

HashMap<Rectangle, String> map2 = new HashMap<>(16);
map2.put(rectangle1, "nice");
map2.put(rectangle2, "bad");
System.out.println("size:"+map2.size());
输出结果:size:2

We have considered rectangle1 and rectangle2 is logically the same, that is, rectangle1 and rectangle2 is the same key, and add rectangle2 in put method when, "bad" should override the "nice", size should be 1, but the truth concurrent hashCode method but we think this way, when the subject of the hash table, it will call this object in the array of computing, if we do not cover the hashCode method, it will convert the address of the object into a heap when calculating the hash value of an integer hash value, which is on the high probability will rectangle1 and rectangle2 assigned two different hash buckets.

Want to make rectangle1 and rectangle2 as the same key, falls on the same bucket in the hash, we have to override hashCode method

@Override
public int the hashCode () {
17,31 // following numbers are arbitrarily designated size.
Result =. 17 int;
// Here, I simply assigned to the same area of the rectangular, hashCode same value is
Result = Result + 31 is * length * width;
return Result;
}
In this case try again

HashMap<Rectangle, String> map2 = new HashMap<>(16);
map2.put(rectangle1, "nice");
map2.put(rectangle2, "nice");
System.out.println("size:"+map2.size());
输出结果:size:1

This time got the results we want.

After we covered the hashCode method, ask yourself, "We believe that equality of logical instances have equal hash codes." By writing unit tests to verify your inference.

Summary: determination needs to be done for logical equivalence classes, override equals, if required as a key in the hash table (HashMap, HashSet), it is necessary to cover the hashcode method.


Transfer from
the original: https: //blog.csdn.net/gkmmg/article/details/80296238

Guess you like

Origin www.cnblogs.com/xp0813/p/11293996.html