public int hashCode() {
int sum = width + height;
return sum * (sum + 1)/2 + width;
}
int sum = width + height;
return sum * (sum + 1)/2 + width;
}
解决方案 »
- 请问大家能让JScrollPane透明和让jframe窗口总在最前吗?
- 请问 怎样将程序和GUI连接再一起/怎样让bufferedreader System.in记录输入次数?
- 类的初始化问题
- 在环境变量中如何将我的工作目录设为java编译时寻找原文件的默认路径?
- 关于TimerTask类...请指教!!!
- 实在是找不到com.kvisco.xsl.XSLStylesheet
- 这样的一个字符串该如何解析比较好?
- 代码优化!谢谢
- JAVA基础问题
- 使用tomcat出现的问题(socket write error),请教大家
- 新手问题多重继承下的顶层基类的成员怎样直接访问
- 面试中遇到过英文口语考察的同志们进来帮个忙!600里加急!
hascode不能重复的吧
但是a.hashCode() == b.hashCode() -/-> a.equals(b)
return sum * (sum + 1)/2 + width;
为什么能满足hashcode尽量不重复的要求?这里面有什么数学定理或数学常识吗?
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
It is not required that if two objects are unequal according to the equalsmethod, then the two objects must produce distinct hash code.
当你实现了equals的时候,一定要同时生成hashCode,并且当对象相等hashcode也应该相等.
hashcode主要用在散列的时候对查询等效率影响较大,所以如果生成的hashcode 很容易重复的话,一个由你的对象所组成的HashMap(HashTable)效率就会非常的低.至于hashCode采用什么方法生成并没有特殊规定,只要满足上边几个条件就行了.