针对hashmap中某个entry链太长,查找的时间复杂度可能达到o,怎么优化
答案:1 悬赏:20 手机版
解决时间 2021-11-17 10:57
- 提问者网友:溺爱和你
- 2021-11-16 19:37
针对hashmap中某个entry链太长,查找的时间复杂度可能达到o,怎么优化
最佳答案
- 五星知识达人网友:詩光轨車
- 2021-11-16 20:04
containsKey的复杂度是O(1),它是直接根据给定的参数key来计算hashcode,看看相关位置上是否有。如果相关位置已被占用,就继续寻找下一个位置。下面是JDK实现containsKey的主要代码:
int hash = hash(k);
int i = indexFor(hash, table.length);
Entry e = table[i];
while (e != null) {
if (e.hash == hash && eq(k, e.key))
return true;
e = e.next;
}
int hash = hash(k);
int i = indexFor(hash, table.length);
Entry e = table[i];
while (e != null) {
if (e.hash == hash && eq(k, e.key))
return true;
e = e.next;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯