为什么重载equals时一定要重载hashCode方法,看下面的测试,首先仅重载equals方法,而不重载hashCode方法
package com.design.hash;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestEntity {
private String entityName;
private int start;
public String getEntityName() {
return entityName;
}
public void setEntityName(String entityName) {
this.entityName = entityName;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TestEntity other = (TestEntity) obj;
if (entityName == null) {
if (other.entityName != null)
return false;
} else if (!entityName.equals(other.entityName))
return false;
if (start != other.start)
return false;
return true;
}
public static void main(String[] args){
List<TestEntity> entityList = new ArrayList<TestEntity>();
Map<TestEntity, String> testMap = new HashMap<TestEntity, String>();
TestEntity entity = new TestEntity();
entity.setEntityName("test");
entity.setStart(111);
TestEntity entity1 = new TestEntity();
entity1.setEntityName("test");
entity1.setStart(111);
entityList.add(entity);
testMap.put(entity, "OK");
System.out.println("HashMap:" + testMap.containsKey(entity1));
System.out.println("List:" + entityList.contains(entity1));
}
}
HashMap:false
List:true
可以看到在List使用中是没有任何问题的,但是在HashMap中不可以,JDK源码中可以看到HashMap是基于hash的,会调用对象的hashCode方法,所以结果是false,而List不是基于hash的,不调用hashCode,所以没有问题,结论就是如果对象仅用于List等非hash的集合中,只override其equals方法不会有问题,但是我们难保不将对象放中hash的集合中,所以同时重载hashCode是有必要的,否则这样的问题在后期难以被查出。
package com.design.hash;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestEntity {
private String entityName;
private int start;
public String getEntityName() {
return entityName;
}
public void setEntityName(String entityName) {
this.entityName = entityName;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((entityName == null) ? 0 : entityName.hashCode());
result = prime * result + start;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TestEntity other = (TestEntity) obj;
if (entityName == null) {
if (other.entityName != null)
return false;
} else if (!entityName.equals(other.entityName))
return false;
if (start != other.start)
return false;
return true;
}
public static void main(String[] args){
List<TestEntity> entityList = new ArrayList<TestEntity>();
Map<TestEntity, String> testMap = new HashMap<TestEntity, String>();
TestEntity entity = new TestEntity();
entity.setEntityName("test");
entity.setStart(111);
TestEntity entity1 = new TestEntity();
entity1.setEntityName("test");
entity1.setStart(111);
entityList.add(entity);
testMap.put(entity, "OK");
System.out.println("HashMap:" + testMap.containsKey(entity1));
System.out.println("List:" + entityList.contains(entity1));
}
}