equals与hashCode

     为什么重载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));
	}
	
	
}
 


转载于:https://my.oschina.net/u/1038535/blog/184099

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值