The equals() and hashcode() methods for object comparison in Java

equals() and hashcode() in Java object comparison 1. Comparison and analysis of equals() method and "= =" First, put forward a misunderstanding: ==Address comparison during comparisonValues are compared when equals is compared String a = "123"; String b = "123"; System.out.println(a.equals(b)); // true System.out.println(a == b); ...

Posted by 28rain on Tue, 18 Jan 2022 22:33:57 +0100

How and why does Java override the equals and hashCode methods of objects

Foreword: if Java objects want to compare whether they are equal, they need to rewrite the equals method and the hashCode method, and the prime number 31 is used in the hashCode method. Let's see why. 1, Requirements: Compare whether two objects are equal. For the following User object, it is considered the same object as long as the name a ...

Posted by Dave3765 on Sat, 15 Jan 2022 04:12:49 +0100