Java|Java @Override hashCode() equals()

Test有三个变量String的name,int的salary,double的weight

public class Test {private final String name; private final int salary; private final double weight;

Constructor
public Test(String name, int salary, double weight) { this.name = name; this.salary = salary; this.weight = weight; }

【Java|Java @Override hashCode() equals()】equals实现
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; }Test test = (Test) o; if (name != null ? !name.equals(test.name) : test.name != null) { return false; } if (!Double.valueOf(weight).equals(Double.valueOf(test.weight))) { return false; } return salary == test.salary; }

hashCode实现
@Override public int hashCode() { int result = (name != null ? name.hashCode() : 0); result = 31 * result + salary; result = 31 * result + Double.valueOf(weight).hashCode(); return result; } }

    推荐阅读