前言:Java 对象如果要比较是否相等,则需要重写 equals 方法,同时重写 hashCode 方法,而且 hashCode 方法里面使用质数 31。接下来看看各种为什么。
一、需求:
对比两个对象是否相等。对于下面的 User 对象,只需姓名和年龄相等则认为是同一个对象。
二、解决方案:
需要重写对象的 equals 方法和 hashCode 方法
package com.yule.user.entity;
import org.springframework.util.StringUtils;
public class User {