导语
手撕面试官,面试某公司开发的时候被问到了HashMap底层,问到我怀疑人生,不知道是面试官错了还是我错了。我相信是我错了利用下班时间来分析手撕一下HashMap。
通过手撕源码加上实验来给自己打脸,这些东西你跟面试官真的懂么?想要做出创新的东西真的容易么?就靠着自己对于增删改查就可以驰骋江湖了么?自己离高手的路还有一段距离。
实验
不用多说直接上源码这里,首先解释一下传入的几个参数。HashMap 传入的就是KV两个值,没有什么好解释的,进入方法之后调用的是putVal的方法这里可以看到。调用了一个hash()的方法。
/**
* Associates the specified value with the specified key in this map.
* If the map previously contained a mapping for the key, the old
* value is replaced.
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @return the previous value associated with <tt>key</tt>, or
* <tt>null</tt> if there was no mapping for <tt>key</tt>.
* (A <tt>null</tt> return can also indicate that the map
*
本文深入剖析HashMap的putVal()、resize()、treeifyBin()等核心方法,通过实验揭示HashMap的工作原理。分析了HashCode的生成、链表到红黑树的转换,并指出真正理解HashMap的性能瓶颈和优化点对于开发者的重要性。
订阅专栏 解锁全文
968

被折叠的 条评论
为什么被折叠?



