Why must override hashCode method When overridden equals

1, hashCode role is to obtain the hash code, also called a hash code, the object is to determine the index location in the hash table, the value is a type of int.

2. If two objects are equal, then hashCode must be equal, but equal does not mean that hashCode two objects are equal.

3, HashSet example, when a new object is added HserhSet first determines whether a new object hashCode hashCode the same set of objects, is being added if not identical, the same is again true for determining whether the two objects with equals equal, thus reducing the call equals and improve efficiency.

4, the effect equals that determines whether two objects are equal, "==" is the address of the object is determined whether the two are equal.

Guess you like

Origin www.cnblogs.com/duanxiansen/p/12057107.html