key

Key

当一个类的实例作为HashMap的key时,它的equals方法与hashcode方法的重写直接影响着散列表	(HashMap)的查询性能。
在API文档中Object对这两个方法的重写做了说明:
当我们重写一个类的equals方法时,就应当连同重写hashcode方法。
这两个方法的重写应当遵循:
1:一致性,当两个对象equals比较为true时,hashcode方法返回的数字必须相等。反过来虽然不是必须的,但也应当遵循,否则在HashMap中会形成链表影响查询性能,所以两个对象hashcode值相同,equals比较也应当为true
2:稳定性:hashcode方法多次调用后返回的数字应当相同,不应是一个变化的值,除非参与equals比较的属性值发生了变化。

eg:

public class Key {
	private int x;
	private int y;
	
	public Key(int x, int y) {
		super();
		this.x = x;
		this.y = y;
	}
	
	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + x;
		result = prime * result + y;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Key other = (Key) obj;
		if (x != other.x)
			return false;
		if (y != other.y)
			return false;
		return true;
	}
	

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值