hashCode equals == problem resolution

The difference and connection between hashCode and equals

The difference between == sign and equals() method in Java

  • equals() The hashCode() of two objects that are equal must be equal, that is, the comparison with equals() is absolutely reliable.
  • Two objects with equal hashCode() have equals() not necessarily equal, that is, hashCode() is not absolutely reliable.
  • If you need a lot of and fast comparison, if you use equals() to do it is obviously too inefficient, so the solution is, whenever you need to compare, first use hashCode() to compare, if the hashCode() is not the same, then Indicates that these two objects are definitely not equal (that is, no need to use equals() to compare again), if the hashCode() is the same, then compare their equals(), if equals() is also the same, it means that the two The objects are really the same, which not only greatly improves the efficiency but also ensures the absolute correctness of the comparison! ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

  • If the equals(Object obj) method is overridden, it is necessary to override the hashCode() method.
  • If two objects equals(Object obj) return true, then hashCode() must also return the same int number.
  • If two objects equals (Object obj) return false, then hashCode() does not necessarily return different int numbers.
  • If two objects hashCode() return the same int number, equals(Object obj) may not return true.
  • If two object hashCode() return different int numbers, equals(Object obj) must return false.
  • If the same object has been stored in the collection during execution, the related information that affects the hashCode value cannot be modified, otherwise it will cause memory leaks.
  • The hashcode method is only used in the collection. When putting an object into the collection, first judge whether the hashcode value of the object to be put into the collection is equal to the hashcode value of any element in the collection, and if not, put the object directly into the collection. in. If the hashcode values ​​are equal, then the equals() method is used to determine whether the object to be placed is equal to any object in the collection. If equals() judges that it is not equal, the element is directly placed into the collection, otherwise it is not placed.
  • The equals() method is used to compare whether the contents of the object are equal.

 

Guess you like

Origin blog.csdn.net/xiangwang2016/article/details/100560296