如何正确的重写equals() 和 hashCode()方法

本文将介绍在java中如何重写equals 和hashCode方法。

---译自:http://www.mkyong.com/java/java-how-to-overrides-equals-and-hashcode/

比较两个Java对象时, 我们需要覆盖equals和  hashCode

public class User{    
    private String name;
    private int age;
    private String passport;

	//getters and setters, constructor
}

在比较结果时:
    User user1 = new User("mkyong", 35, "111222333");
    User user2 = new User("mkyong", 35, "111222333");

    System.out.println(user1.equals(user2)); // false

下面我们将介绍几种常用方法:

 1.经典方式

这种17和31散列码的想法来自经典的Java书籍——《Effective Java》第九条。下面我们来看看是如何实现的...
public class User {
    private String name;
    private int age;
    private String passport;
    //getters and setters, constructor
    @Override
    public boolean equals(Object o) {
        if (o == this) return true;
        if (!(o instanceof User)) {
            return false;
        }
        User user = (User) o;
        return user.name.equals(name) &&
                user.age == age &&
                user.passport.equals(passport);
    }
    //Idea from effective Java : Item 9
    @Override
    public int hashCode() {
        int result = 17;
        result = 31 * result + name.hashCode();
        result = 31 * result + age;
        result = 31 * result + passport.hashCode();
        return result;
    }
}

2.JDK 7

对于JDK7及更新版本,你可以是使用java.util.Objects 来重写 equals 和 hashCode 方法,代码如下

import java.util.Objects;

public class User {
    private String name;
    private int age;
    private String passport;

    //getters and setters, constructor

    @Override
    public boolean equals(Object o) {
        if (o == this) return true;
        if (!(o instanceof User)) {
            return false;
        }
        User user = (User) o;
        return age == user.age &&
                Objects.equals(name, user.name) &&
                Objects.equals(passport, user.passport);
    }

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

}

3.Apache Commons Lang

或者,您可以使用Apache Commons LangEqualsBuilder HashCodeBuilder 方法。代码如下
import org.apache.commons.lang3.builder;

public class User {
    private String name;
    private int age;
    private String passport;
    //getters and setters, constructor

     @Override
    public boolean equals(Object o) {

        if (o == this) return true;
        if (!(o instanceof User)) {
            return false;
        }
        User user = (User) o;

        return new EqualsBuilder()
                .append(age, user.age)
                .append(name, user.name)
                .append(passport, user.passport)
                .isEquals();
    }
    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37)
                .append(name)
                .append(age)
                .append(passport)
                .toHashCode();
    }
}

最后测试总结:

在使用上述三种任何一种方式都可以到如下结果:
    User user1 = new User("mkyong", 35, "111222333");
    User user2 = new User("mkyong", 35, "111222333");
    System.out.println(user1.equals(user2)); // true

其实后两种都是对于17和31散列码思想的封装实现。具体请参考《Effective Java》第九条。


你可能感兴趣的:(java基础)