Java关于集合的疑惑
class Student 
{
	private String name;
	private int age;
	Student(String name,int age )
	{
		this.name=name;
		this.age=age;
	}
	public String GetName()
	{
		return this.name;
	}
	public int Getage()
	{
		return this.age;
	}
}
class Demo
{
   HashSet<Student> hs=new HashSet<Student>;
   hs.add("yang",23);
   hs.add("shu",22);
   hs.add("yang",23);
}
现在的问题是,书上说HashSet集合首先根据集合中元素的hashcode值,然后通过equals方法来确保元素的唯一性,当然了equals方法是在hashcode值相同的情况下才执行,但是查阅Api时候,文档说hashset集合有继承自java.util.AbstractSet的hashCode方法,那么为什么不通过对象来调用集合的hahcode方法来实现保证元素的唯一性,为什么我们只需在Student类中重写hashcode()即可,这到底是怎么在运行?
------解决方案--------------------对象调用hashcode()确定不了元素的惟一性。
元素的唯一应该要包括:地址唯一,内容唯一,对象所在的类唯一吧。
------解决方案--------------------建议了解一下Hash算法、看一下HashSet和HashMap的源码,因为HashSet是用HashMap实现的,看一下数据在HashMap中到底是怎么存储的,就明白了。
------解决方案--------------------也木有啥书籍可以推荐的。我也很菜。一起努力吧!
