什么情况下需要重写equals方法,为什么一定需要重写hashcode

一.什么情况下需要重写equals方法?

1.正常情况下,Java的基本数据类型和包装数据类型都已经重写的Object类的equals方法和hashCode方法
2.已经实现了我们想要的目的:只考虑name属性是否相同,不考虑studentId属性的异同来判断Student对象是否相等

需要重写equals方法的场景:主要依据我们的业务场景来看,例如:当我们对Student这个对象进行判断的时候,我们不需要Student的所有的属性都必须相同,则可以重写Student的equals方法,和hashCode()方法,达到只判断Student的name相同即为对象相同,而不需要考虑其他的id等属性是否相同.

二、为什么重写 equals 还要重写 hashcode?

在Java中,如果你重写了equals()方法,就必须重写hashCode()方法,这是因为在Java的集合框架(如HashMap、HashSet等)中,hashCode()和equals()方法是紧密相关的,遵循以下两个重要的规则:

1.相等对象必须具有相等的哈希码:如果根据equals()方法,两个对象是相等的,那么它们的hashCode()也必须相等。否则,这些对象在基于哈希的集合(如HashSet、HashMap)中会表现出不可预期的行为,例如两个相等的对象可能会存储在集合的不同位置,从而导致数据丢失或无法正确检索。

2.不相等的对象不需要有不同的哈希码:虽然这不是强制要求的,但如果两个不相等的对象有不同的哈希码,可以提高哈希表的性能,因为这会减少哈希冲突。

import java.util.Objects;

public class Student {
    private String name;
    private String studentId;

    public Student(String name, String studentId) {
        this.name = name;
        this.studentId = studentId;
    }

    @Override
    public boolean equals(Object o) {
       /*
       要重写 equals() 方法,通常遵循以下步骤:
       1. 检查引用:首先检查是否是同一个对象。
       2. 检查类型:确保传入的对象与当前对象属于相同的类。
       3. 强制类型转换:将传入的对象转换为当前类的类型。
       4.比较字段:逐个比较对象的字段*/
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(name, student.name) && Objects.equals(studentId, student.studentId);
    }

    @Override
    public int hashCode() {
        /*
         对于重写 equals() 方法的类,必须重写 hashCode() 方法,以确保相等的对象具有相同的哈希码。通常遵循以下步骤:
         1.使用字段生成哈希值:选择参与相等性比较的字段来计算哈希值。
         2.使用合适的算法:通常使用乘法和加法的组合来生成哈希值。
         */
        return Objects.hash(name, studentId);
    }

    public static void main(String[] args) {
        // 创建学生对象
        Student student1 = new Student("Alice", "1001");
        Student student2 = new Student("Alice", "1001");
        Student student3 = new Student("Bob", "1002");
        Student student4 = new Student("Alice", "1003");

        // 比较学生对象的 equals 方法
        System.out.println("Comparing student1 and student2: " + student1.equals(student2)); // true
        System.out.println("Comparing student1 and student3: " + student1.equals(student3)); // false
        System.out.println("Comparing student1 and student4: " + student1.equals(student4)); // false

        // 比较学生对象的 hashCode 方法
        System.out.println("Hash code of student1: " + student1.hashCode());
        System.out.println("Hash code of student2: " + student2.hashCode());
        System.out.println("Hash code of student3: " + student3.hashCode());
        System.out.println("Hash code of student4: " + student4.hashCode());

        // 检查 hashCode 和 equals 方法的一致性
        System.out.println("Hash codes are equal for student1 and student2: " +
                (student1.hashCode() == student2.hashCode())); // true
        System.out.println("Hash codes are equal for student1 and student3: " +
                (student1.hashCode() == student3.hashCode())); // false
    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容