package com.heatdeath.object;
import lombok.extern.slf4j.Slf4j;
import java.util.HashMap;
import java.util.Map;
/**
* Author: heatdeath
* Date: 2018/4/19
* Desc:
*/
@Slf4j
public class EqualsDemo {
public static void main(String[] args) {
MyEqualsDemo demo_1 = new MyEqualsDemo("111", "111");
MyEqualsDemo demo_2 = new MyEqualsDemo("222", "222");
MyEqualsDemo demo_3 = new MyEqualsDemo("111", "111");
log.info("demo_1 hashcode is {}", demo_1.hashCode());
log.info("demo_2 hashcode is {}", demo_2.hashCode());
log.info("demo_3 hashcode is {}", demo_3.hashCode());
log.info("demo_1 toString is {}", demo_1.toString());
log.info("demo_2 toString is {}", demo_2.toString());
log.info("demo_3 toString is {}", demo_3.toString());
log.info("demo_1 == demo_3 result is {}", demo_1 == demo_3); // False
log.info("demo_1.equals(demo_3) result is {}", demo_1.equals(demo_3)); // True
log.info("-----------------------------------------------------");
// Map<MyEqualsDemo, String> map = new HashMap<>();
// map.put(demo_1, "demo_1");
// log.info("map.get(demo_1) is {}", map.get(demo_1));
//
// demo_1.setField_1("change field_1");
// log.info("now demo_1 hashcode is {}", demo_1.hashCode());
// log.info("map.get(demo_1) is {}", map.get(demo_1));
//
// Map<int, String> map1 = new HashMap<>();
// Map<String, int> map2 = new HashMap<>();
}
}
class MyEqualsDemo {
private String field_1;
private String field_2;
public MyEqualsDemo(String field_1, String field_2) {
this.field_1 = field_1;
this.field_2 = field_2;
}
public String getField_1() {
return field_1;
}
public void setField_1(String field_1) {
this.field_1 = field_1;
}
public String getField_2() {
return field_2;
}
public void setField_2(String field_2) {
this.field_2 = field_2;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyEqualsDemo that = (MyEqualsDemo) o;
return (field_1 != null ? field_1.equals(that.field_1) : that.field_1 == null) && (field_2 != null ? field_2.equals(that.field_2) : that.field_2 == null);
}
@Override
public int hashCode() {
int result = field_1 != null ? field_1.hashCode() : 0;
result = 31 * result + (field_2 != null ? field_2.hashCode() : 0);
return result;
}
}
21:33:30.309 [main] INFO com.heatdeath.object.EqualsDemo - demo_1 hashcode is 1557024
21:33:30.322 [main] INFO com.heatdeath.object.EqualsDemo - demo_2 hashcode is 1588800
21:33:30.322 [main] INFO com.heatdeath.object.EqualsDemo - demo_3 hashcode is 1557024
21:33:30.322 [main] INFO com.heatdeath.object.EqualsDemo - demo_1 toString is com.heatdeath.object.MyEqualsDemo@17c220
21:33:30.322 [main] INFO com.heatdeath.object.EqualsDemo - demo_2 toString is com.heatdeath.object.MyEqualsDemo@183e40
21:33:30.322 [main] INFO com.heatdeath.object.EqualsDemo - demo_3 toString is com.heatdeath.object.MyEqualsDemo@17c220
21:33:30.322 [main] INFO com.heatdeath.object.EqualsDemo - demo_1 == demo_3 result is false
21:33:30.322 [main] INFO com.heatdeath.object.EqualsDemo - demo_1.equals(demo_3) result is true
21:33:30.322 [main] INFO com.heatdeath.object.EqualsDemo - -----------------------------------------------------
结论:
1、如果 demo_1.equals(demo_3)
为 true,则 demo_1 与 demo_3 的 hashCode 必定相同
2、如果两个对象的 hashCode 相同,但 equals() 不一定返回 true
3、 equals 比较的是对象的内容, == 比较的是引用类型数据所指向堆中对象的地址
这个是别人的结论,好像比我的更精炼一些…
1)对于 == 关系操作符,如果作用于基本数据类型的变量,则直接比较其存储的 “值”是否相等;
如果作用于引用类型的变量,则比较的是所指向的对象的地址
2)对于 equals 方法,注意: equals 方法不能作用于基本数据类型的变量
如果没有对 equals 方法进行重写,则比较的是引用类型的变量所指向的对象的地址;
诸如 String、Date 等类对 equals 方法进行了重写的话,比较的是所指向的对象的内容。