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;
}
}
推荐阅读
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 事件代理
- Java|Java OpenCV图像处理之SIFT角点检测详解
- java中如何实现重建二叉树
- 数组常用方法一
- 【Hadoop踩雷】Mac下安装Hadoop3以及Java版本问题
- Java|Java基础——数组
- RxJava|RxJava 在Android项目中的使用(一)
- java之static、static|java之static、static final、final的区别与应用
- Java基础-高级特性-枚举实现状态机