Copyright: original works, for permission to reproduce, reprint, please be sure to hyperlink to the original source of the article indicated, author information and this statement. Otherwise held liable. http://zhangjunhd.blog.51cto.com/113473/71571

The following summary from the "Effective Java".

1. When you need to override equals ()

When a class has its own unique "logical equal" concept (as opposed to the concept of object identity.)

2. Design equals ()

[1] using the instanceof operator to check the "argument of whether the correct type."

[2] For each class a "key field", to check the arguments in the current object in the domain and the corresponding field values.

[2.1] for non-float and double primitive type type field, use the == comparison;

[2.2] for object reference domain, recursive calls equals method;

[2.3] for float field, use the Float. FloatToIntBits (afloat) is converted to int, then use the == comparison;

[2.4] for the double domains, use the Double. DoubleToLongBits (adouble) converted to int, then use the == comparison;

[2.5] for the array of domain, call Arrays.equals method.

3. When override equals (), you always want to rewrite the hashCode ()

According to a class equals method (rewritten), there may be two distinct instances are equal in terms of logic, but, according Object.hashCode methods, they are only two objects. Therefore, a violation of "equal objects must have equal hash codes."

4. Design hashCode ()

[1] to a non-zero constant value, such as 17, the result stored in the int variable;

[2] For each of the key objects in the domain f (equals methods that consider each domain):

[2.1] boolean type, calculated (f? 0: 1);

[2.2] byte, char, short type, calculate (int);

[2.3] long type, calculate (int) (f ^ (f>>> 32));

[2.4] float-type, calculated Float. FloatToIntBits (afloat);

[2.5] double type, calculated Double. DoubleToLongBits (adouble) to get a long, then the implementation of [2.3];

[2.6] object reference, recursively call its hashCode method;

[2.7] array domain, each element of which is called its hashCode method.

[3] to calculate the hash code above saved to int variable c, then run result = 37 * result + c;

[4] return result.

5. Example

Following the design of the class to follow the above principles, the class overrides the equals () and hashCode ().

package com.zj.unit;

import java.util.Arrays;

public class Unit {

private short ashort;

private char achar;

private byte abyte;

private boolean abool;

private long along;

private float afloat;

private double adouble;

private Unit aObject;

private int [] ints;

private Unit [] units;

public boolean equals (Object o) {

if (! (o instanceof Unit))

return false;

Unit unit = (Unit) o;

return unit.ashort == ashort

& & Unit.achar == achar

& & Unit.abyte == abyte

& & Unit.abool == abool

& & Unit.along == along

& & Float. FloatToIntBits (unit.afloat) == Float

. FloatToIntBits (afloat)

& & Double. DoubleToLongBits (unit.adouble) == Double

. DoubleToLongBits (adouble)

& & Unit.aObject.equals (aObject)

& & EqualsInts (unit.ints)

& & EqualsUnits (unit.units);

}

private boolean equalsInts (int [] aints) {

return Arrays. equals (ints, aints);

}

private boolean equalsUnits (Unit [] aUnits) {

return Arrays. equals (units, aUnits);

}

public int hashCode () {

int result = 17;

result = 37 * result + (int) ashort;

result = 37 * result + (int) achar;

result = 37 * result + (int) abyte;

result = 37 * result + (abool? 0: 1);

result = 37 * result + (int) (along ^ (along>>> 32));

result = 37 * result + Float. floatToIntBits (afloat);

long tolong = Double. doubleToLongBits (adouble);

result = 37 * result + (int) (tolong ^ (tolong>>> 32));

result = 37 * result + aObject.hashCode ();

result = 37 * result + intsHashCode (ints);

result = 37 * result + unitsHashCode (units);

return result;

}

private int intsHashCode (int [] aints) {

int result = 17;

for (int i = 0; i <aints.length; i + +)

result = 37 * result + aints [i];

return result;

}

private int unitsHashCode (Unit [] aUnits) {

int result = 17;

for (int i = 0; i <aUnits.length; i + +)

result = 37 * result + aUnits [i]. hashCode ();

return result;

}

}

Copyright: original works, for permission to reproduce, reprint, please be sure to hyperlink to the original source of the article indicated, author information and this statement. Otherwise held liable. http://zhangjunhd.blog.51cto.com/113473/71571

The following summary from the "Effective Java".

1. When you need to override equals ()

When a class has its own unique "logical equal" concept (as opposed to the concept of object identity.)

2. Design equals ()

[1] using the instanceof operator to check the "argument of whether the correct type."

[2] For each class a "key field", to check the arguments in the current object in the domain and the corresponding field values.

[2.1] for non-float and double primitive type type field, use the == comparison;

[2.2] for object reference domain, recursive calls equals method;

[2.3] for float field, use the Float. FloatToIntBits (afloat) is converted to int, then use the == comparison;

[2.4] for the double domains, use the Double. DoubleToLongBits (adouble) converted to int, then use the == comparison;

[2.5] for the array of domain, call Arrays.equals method.

3. When override equals (), you always want to rewrite the hashCode ()

According to a class equals method (rewritten), there may be two distinct instances are equal in terms of logic, but, according Object.hashCode methods, they are only two objects. Therefore, a violation of "equal objects must have equal hash codes."

4. Design hashCode ()

[1] to a non-zero constant value, such as 17, the result stored in the int variable;

[2] For each of the key objects in the domain f (equals methods that consider each domain):

[2.1] boolean type, calculated (f? 0: 1);

[2.2] byte, char, short type, calculate (int);

[2.3] long type, calculate (int) (f ^ (f>>> 32));

[2.4] float-type, calculated Float. FloatToIntBits (afloat);

[2.5] double type, calculated Double. DoubleToLongBits (adouble) to get a long, then the implementation of [2.3];

[2.6] object reference, recursively call its hashCode method;

[2.7] array domain, each element of which is called its hashCode method.

[3] to calculate the hash code above saved to int variable c, then run result = 37 * result + c;

[4] return result.

5. Example

Following the design of the class to follow the above principles, the class overrides the equals () and hashCode ().

Copyright: original works, for permission to reproduce, reprint, please be sure to hyperlink to the original source of the article indicated, author information and this statement. Otherwise held liable. http://zhangjunhd.blog.51cto.com/113473/71571

1, the first hashCode () is a method of Object class API, the following instructions: public int hashCode () returns the object's hash code value. Support for the hash table is to provide some advantages, such as, java.util.Hashtable hash table provi

【Zhuantie - not forgetting the original quote address is recorded, please forgive me】 Value types Value types are stored in the memory stack (hereafter referred to as the stack), while the reference type of a variable on the stack only reference type

When we write a class when the class will be on Java.lang.Object rewrite some of the important methods, these methods include: hashCode (), toString (), equals (), finalize (), clone (), wait ( ), notify () / notifyAll () method of the eight. One Equ

Fast Translation] [51CTO This paper introduces a rewritten (override) equals method for skills. The use of the technique, even if a subclass of the entity to add a new field (field), there is still equals methods to meet the contract. In "Effective J

Transfer from: http://www.javaresearch.org/html/article/46110.htm Summarized in one sentence: == to compare two object references (ie, memory address) is equal, and equals () comparison is the value of two objects (ie, memory address where stored val

Value types are stored in the memory stack (after the short stack), and a reference type variable is only stored on the stack the address of a reference type variable, and then stored in the heap of its own. == Operator compare the two variables are

equals the true time, hashcode must be equal, this rule was not previously noted, each override equals methods are not considered hahsCode, write here to make a mark. If you override equals, but not modify the hashCode, HashMap, and the like in the u

Hash functions that had previously been very profound, on the Wang Shan's "Principles of database implementation," when it seems to understand a little bit, but to learn java Time, frequent contact with hashcode (), hashMap these to always think

Quote In the Java language, equals () and hashCode () using the two functions are closely co-ordinated if you are one of their own design, we must design another one. In most cases, these two functions are not considered, the direct use of their defa

java in the hashcode () and equals () of the Xiang Jie [change] October 30, 2008 Thursday, 11:23 PM Research this afternoon for a long time hashcode () and equals () method, finally had a little bit to understand, write down and share 1. First of equ

1. First equals () and hashcode () These two methods are inherited from object class in the. equals () method in the object class is defined as follows: public boolean equals (Object obj) ( return (this == obj); ) Obviously, the address of the two ob

1. '==' Is used to compare two variables (the basic type and object type) value is equal, if two variables are the basic types, it is very easy to directly compare the value on it. If two variables are the object type, then it is quite the value, but

Quoted from http://www.cnjm.net/tech/article4731.html For a long afternoon of hashcode () and equals () method, finally have a little bit to understand, write it down and share (zhaoxudong 2008.10.23 Late 21.36). 1. First equals () and hashcode () of