返回值不对?
31只是一个经验值吧.
当h越界了,hashcode就有可能重复.但是hashcode并不要求一定是唯一的.
hashcode的目的是减少相同的.
String类是final的,所以不能继承,不能覆写里面的方法.

解决方案 »

  1.   

    hashcode的三大原则:
    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.
      

  2.   

    其实你可以利用string类中的一些方法的使用自己来写一个处理的方法也未尝不可!也可以尝试用stringbuffer来试一试!
      

  3.   

    看一下hash表的原理就知道了,存储长度之类的东西用的值全都是经验值,跟实际情况是有关的,比如说你所存内容的概率分布等。
      

  4.   

    我现在是这样的,比如一个32位 的字符串,我需要经过一个hash算法,减少为16各位有什么好的主意,来讨论一下
      

  5.   

    to :goldenhua(深深地爱上了你) 谢谢关注,能否指点一下md5、sha-1在java中位数都很长,不能满足需要给我提个好的建议吧
      

  6.   

    hash其实是一种多对一的映射,是肯定要丢失信息的;随便你怎么变换一个String吧,只要把结果弄成固定的16位长的整数就可以了;hash函数可以根据数据本身的特性定制,基本的原则是均布性要好,性能要好;至于md5、sha-1等这样的hash函数主要用在加密方面,因为他们是公认的难以枚举冲突的算法,如果只是用于哈希表这样的应用,则是没有必要使用的(效率低下);譬如用下面这个算法:
    将String的每一个char的Unicode值平方一下,累加起来,然后取sum的末16位即可;至于实现不用我写出来吧。
      

  7.   

    to :goldenhua(深深地爱上了你) 
    可以详细点吗?
    多谢了
      

  8.   

    能具体解释一下这个Hashcode吗????????
    看不懂啊