equals()方法和hashCode()方法在HashMap中的应用

在Object类中定义了两个非常重要的方法:

public boolean equals(Object obj)
public int hashCode()

如果我们要向HashMap中添加我们自己定义的类,那么对这两个方法的理解就显得至关重要了。有时候即便是经验丰富的Java开发者也会在这上面犯错误。下面我们就来讲解一下如何在HashMap中正确的使用这几个方法。


来看下面一个例子:
import java.util.*;

class Dog {
	private String name;

	public Dog(String name) {
		this.name = name;
	}

	public boolean equals(Object obj) {
		if(!(obj instanceof Dog))
			return false;

		if(obj == this)
			return true;

		// 这里的强制转换一定是安全的
		// 因为经过了上面 instanceof 的检查
		Dog other = (Dog)obj;
		return other.name.equals(this.name);
	}
}


public class HashTest {
	public static void main(String[] args) {
		Map<Dog, Integer> map1 = new HashMap<Dog, Integer>();

		map1.put(new Dog("Kitty"), 20);
		map1.put(new Dog("Paul"), 10);

		System.out.println(map1.get(new Dog("Kitty"))); // null

	}
}

在这个例子中,我们将一个名为 "Kitty" 的 Dog 对象放到了HashMap中,但在取出 "Kitty" 的时候却遇到了一个null,即Map中没有这个对象。这是因为我们没有覆盖 hashCode()方法。在重写 equals() 和 hashCode() 时,我们必须遵守以下原则:

1. 如果两个对象是相同的,那么它们必须返回相同的 hashCode。
2. 如果两个对象有相同的 hashCode,它们可能相同也可能不相同。

其原因就在于HashMap的实现方式。先来看一张图:


在数据结构课程中我们都应该学习过哈希表,这里就不再重复了。只是在Java中,HashMap实际上就如同一个二维数组。数组的每一行,就是一个bucket,这个bucket就如同一个链表的头结点,可以用来挂新的结点,并且它有一个名字,这个名字就是 hashCode() 的返回值。在 hashMap中查找对象时要经过两步:一是根据被查找对象的 hashCode() 方法的返回值确定该对象应该在哪个 bucket中,二是对该 bucket 中的对象一个一个地调用 equals() 方法来确定里面有没有要查找的对象。在Java中,Object类默认的 hashCode() 实现是 对每一个对象都返回一个不会重复的整数。我们在向map中添加Dog对象时该对象会被放到A这个bucket中,而在查找时却会在B bucket中去找,这当然不可能找到了。所以,我们才需要重写 hashCode() 方法来保证两个相同的对象都在同一个 bucket 里。

下面是正确的代码:
import java.util.*;


class RobustDog {
	private String name;

	public RobustDog(String name) {
		this.name = name;
	}

	public boolean equals(Object obj) {
		if(!(obj instanceof RobustDog))
			return false;

		if(obj == this)
			return true;

		RobustDog other = (RobustDog)obj;
		return other.name.equals(this.name);
	}

	@Override
	public int hashCode() {
		return this.name.hashCode();
	}

}

public class HashTest {
	public static void main(String[] args) {
		


		Map<RobustDog, Integer> map2 = new HashMap<RobustDog, Integer>();

		map2.put(new RobustDog("Sky"), 20);
		map2.put(new RobustDog("Seam"), 0);
		
		System.out.println(map2.get(new RobustDog("Sky"))); // 结果是 20
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值