为什么equals()和hashcode()方法要同时重写?

一般来说我们重写equals()方法会用的比较多,我们在比较两个自定义类型的变量的时候,比如有这样一个自定义的类型Student:9)

package com.springstudy.testimport.pojo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import java.util.Objects;

@Component
public class Student implements Cloneable{
    @Override
    public Object clone() throws CloneNotSupportedException {
        return (Student)super.clone();
    }

    private String name;
    private Integer age;

    public Student(String name,Integer age){
        this.name=name;
        this.age=age;
    }
    public Student(){}
    public String getName() {
        return name;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getAge() {
        return age;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Bean
    @Scope(value = WebApplicationContext.SCOPE_SESSION,proxyMode = ScopedProxyMode.TARGET_CLASS)
    public Student cacheStudent(){
        return new Student();
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return name.equals(student.name) && age.equals(student.age);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

在比较两个Student对象的时候,如果直接用==符号判断,那肯定是不相等的,(因为我们知道这两个引用变量指向的是两个不同的new出来的对象)但我们一般不希望两个Student这样比较是否相等,我们一般希望比较两个对象的具体信息.所以因为有这个需求,所以重写了equals方法.然后判相等的时候使用.equals()来判断.

那为什么一定要再去重写一下hashcode()方法呢?

因为为了满足Set集合存储元素原则的需要.因为我们希望Set集合在存储Student对象的时候也是通过像equals()方法那样去去重.

具体的分析hashcode()方法和equals()方法,可以参考下面这篇博客:https://blog.csdn.net/dyingstraw/article/details/88041905?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164204358116780255220479%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=164204358116780255220479&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-1-88041905.first_rank_v2_pc_rank_v29&utm_term=%E4%B8%BA%E4%BB%80%E4%B9%88equal%E5%92%8Chashcode%E7%9A%84%E7%BB%93%E6%9E%9C%E4%B8%80%E5%AE%9A%E8%A6%81%E4%B8%80%E8%87%B4&spm=1018.2226.3001.4187

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_34116044

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值