• hashCode 和 equals 方法


    hashCode 和 equals 方法

    hashCode()和equals()定义在Object类中,这个类是所有java类的基类,所以所有的java类都继承这两个方法。

    使用hashCode()和equals()

    hashCode()方法被用来获取给定对象的唯一整数。这个整数被用来确定对象被存储在HashTable类似的结构中的位置。默认的,Object类的hashCode()方法返回这个对象存储的内存地址的编号。

    重写默认的实现

    如果你不重写这两个方法,将几乎不遇到任何问题,但是有的时候程序要求我们必须改变一些对象的默认实现。

    来看看这个例子,让我们创建一个简单的类Employee

    public class Employee
    {
        private Integer id;
        private String firstname;
        private String lastName;
        private String department;
     
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getFirstname() {
            return firstname;
        }
        public void setFirstname(String firstname) {
            this.firstname = firstname;
        }
        public String getLastName() {
            return lastName;
        }
        public void setLastName(String lastName) {
            this.lastName = lastName;
        }
        public String getDepartment() {
            return department;
        }
        public void setDepartment(String department) {
            this.department = department;
        }
    }
    

    上面的Employee类只是有一些非常基础的属性和getter、setter.现在来考虑一个你需要比较两个employee的情形

    public class EqualsTest {
        public static void main(String[] args) {
            Employee e1 = new Employee();
            Employee e2 = new Employee();
     
            e1.setId(100);
            e2.setId(100);
            //Prints false in console
            System.out.println(e1.equals(e2));
        }
    }
    

    毫无疑问,上面的程序将输出false,但是,事实上上面两个对象代表的是通过一个employee。真正的商业逻辑希望我们返回true。
    为了达到这个目的,我们需要重写equals方法。

    public boolean equals(Object o) {
            if(o == null)
            {
                return false;
            }
            if (o == this)
            {
               return true;
            }
            if (getClass() != o.getClass())
            {
                return false;
            }
            Employee e = (Employee) o;
            return (this.getId() == e.getId());
    }
    
    

    在上面的类中添加这个方法,EauqlsTest将会输出true。
    So are we done?没有,让我们换一种测试方法来看看。

    import java.util.HashSet;
    import java.util.Set;
     
    public class EqualsTest
    {
        public static void main(String[] args)
        {
            Employee e1 = new Employee();
            Employee e2 = new Employee();
     
            e1.setId(100);
            e2.setId(100);
     
            //Prints 'true'
            System.out.println(e1.equals(e2));
     
            Set<Employee> employees = new HashSet<Employee>();
            employees.add(e1);
            employees.add(e2);
            //Prints two objects
            System.out.println(employees);
        }
    

    上面的程序输出的结果是两个。如果两个employee对象equals返回true,Set中应该只存储一个对象才对,问题在哪里呢?
    我们忘掉了第二个重要的方法hashCode()。就像JDK的Javadoc中所说的一样,如果重写equals()方法必须要重写hashCode()方法。我们加上下面这个方法,程序将执行正确。

    @Override
     public int hashCode()
     {
        final int PRIME = 31;
        int result = 1;
        result = PRIME * result + getId();
        return result;
     }
    

    使用lombok.EqualsAndHashCode

    import lombok.Data;
    import lombok.EqualsAndHashCode;
    @Data
    @EqualsAndHashCode(callSuper = false)
    public class Employee
    {
        private Integer id;
        private String firstname;
        private String lastName;
        private String department;
    }
    

    EqualsAndHashCode 注解的作用有下面几点:

    1. 此注解会生成equals(Object other) 和 hashCode()方法。
    2. 它默认使用非静态,非瞬态的属性
    3. 可通过参数exclude排除一些属性
    4. 可通过参数of指定仅使用哪些属性
    5. 它默认仅使用该类中定义的属性且不调用父类的方法
    6. 可通过callSuper=true解决上一点问题。让其生成的方法中调用父类的方法

    另:@Data相当于@Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode这5个注解的合集。

    通过官方文档,可以得知,当使用@Data注解时,则有了@EqualsAndHashCode注解,那么就会在此类中存在equals(Object other) 和 hashCode()方法,且不会使用父类的属性,这就导致了可能的问题。
    比如,有多个类有相同的部分属性,把它们定义到父类中,恰好id(数据库主键)也在父类中,那么就会存在部分对象在比较时,它们并不相等,却因为lombok自动生成的equals(Object other) 和 hashCode()方法判定为相等,从而导致出错。

    修复此问题的方法很简单:

    1. 使用@Getter @Setter @ToString代替@Data并且自定义equals(Object other) 和 hashCode()方法,比如有些类只需要判断主键id是否相等即足矣。
    2. 或者使用在使用@Data时同时加上@EqualsAndHashCode(callSuper=true)注解。
  • 相关阅读:
    QT内置的ICON资源
    Spark源代码阅读笔记之MetadataCleaner
    Android API Guides---Bluetooth
    做一个WINDOWS下破解WIFI。不须要Linux抓包!
    CPU GPU设计工作原理《转》
    杭电 1280 前m大的数
    机房收费系统——报表(2)
    概览C++之const
    Android动态禁用或开启屏幕旋转工具
    shrink-to-fit(自适应宽度)
  • 原文地址:https://www.cnblogs.com/SpeakSoftlyLove/p/5603182.html
Copyright © 2020-2023  润新知