Java hashmap如何处理负hashcode?

Java hashmap如何处理负hashcode?,java,collections,hashmap,Java,Collections,Hashmap,Hashcode的类型为int,因此它可以是负数也可以是正数。 但是HashMap使用hashcode来确定表的哪个索引中放置了key+值,比如, hashcode/size\u of\u table=table\u index 但是如果hashcode为负数,HashMap将如何确定它应该在哪个索引中放置key+value?来自: 随便猜一猜,这并不完全是个谜。 static int indexFor(int h, int length) { return h & (lengt

Hashcode的类型为
int
,因此它可以是负数也可以是正数。 但是
HashMap
使用hashcode来确定表的哪个索引中放置了key+值,比如,
hashcode/size\u of\u table=table\u index

但是如果hashcode为负数,
HashMap
将如何确定它应该在哪个索引中放置key+value?

来自:


随便猜一猜,这并不完全是个谜。
static int indexFor(int h, int length) {
    return h & (length-1);
}