返回值不对?
31只是一个经验值吧.
当h越界了,hashcode就有可能重复.但是hashcode并不要求一定是唯一的.
hashcode的目的是减少相同的.
String类是final的,所以不能继承,不能覆写里面的方法.
31只是一个经验值吧.
当h越界了,hashcode就有可能重复.但是hashcode并不要求一定是唯一的.
hashcode的目的是减少相同的.
String类是final的,所以不能继承,不能覆写里面的方法.
1.同样的对象无论什么时候调用hashcode()返回的值都应该相等.
2.如果a.equal(b),那么a.hashcode()应该等于 b.hashcode();
3.如果a.equal(b) == false, 那么不要求a.hashcode() != b.hashcode();
但是应该尽量使得大多数情况下:
如果a.equal(b) == false, 那么a.hashcode() != b.hashcode();Here is the contract, copied from the java.lang.Object specification:
n Whenever it is invoked on the same object more than once during an execution
of an application, the hashCode method must consistently return the
same integer, provided no information used in equals comparisons on the
object is modified. This integer need not remain consistent from one execution
of an application to another execution of the same application.
n 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.
n It is not required that if two objects are unequal according to the equals(Object)
method, then calling the hashCode method on each of the two objects
must produce distinct integer results. However, the programmer should be
aware that producing distinct integer results for unequal objects may improve
the performance of hash tables.
将String的每一个char的Unicode值平方一下,累加起来,然后取sum的末16位即可;至于实现不用我写出来吧。
可以详细点吗?
多谢了
看不懂啊