Why did java rewrite hashCode? when rewriting equals

Why does java rewrite hashCode? when rewriting equals
I think there are two points to pay attention to:

  1. whether hashCode rewriting depends on the business. Open developers can rewrite this method. This may be the case. For example, if we compare only some of the attributes of object, we consider them to be equal without comparing other attributes.
  2. overrides the java object hashCode method to avoid unwanted collisions and collisions in some algorithms. For example, in the use of its HashMap,HashSet.
Mar.14,2021

I think the main point is the second point, avoiding collisions and conflicts.
hashCode is used to identify a single object in a collection class, and I think it is necessary to rewrite it.

and when the programmer's business entity object is created, it may be reasonable to use it, but others do not need to pay special attention to hashCode when using the object.
apache commons provides help methods, which are very useful:

    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }
Menu