Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HashMap(JDK1.8)源码+底层数据结构分析.md中put方法的流程图,转变红黑树时的疑问 #1087

Open
mygao8 opened this issue Feb 17, 2021 · 7 comments
Labels
bug Content error enhancement New feature or request or suggestion

Comments

@mygao8
Copy link

mygao8 commented Feb 17, 2021

屏幕截图 2021-02-17 171926

这里应该是先链表尾插,然后treeifyBin(tab, hash);跳出循环,执行最后面的++modCount;和判断是否需要扩容了吧?

@Snailclimb
Copy link
Owner

屏幕截图 2021-02-17 171926

这里应该是先链表尾插,然后treeifyBin(tab, hash);跳出循环,执行最后面的++modCount;和判断是否需要扩容了吧?

对的 老哥 这里确实存在问题。

@Snailclimb Snailclimb added bug Content error enhancement New feature or request or suggestion labels Feb 25, 2021
@Snailclimb
Copy link
Owner

Snailclimb commented Feb 25, 2021

屏幕截图 2021-02-17 171926

这里应该是先链表尾插,然后treeifyBin(tab, hash);跳出循环,执行最后面的++modCount;和判断是否需要扩容了吧?

已经修改说明!

image

@eazydripz
Copy link

Thank you.

@suozq1
Copy link

suozq1 commented Apr 8, 2022

感觉作者改的,跟问题反馈的没啥关系,树化是在插入链表后,只有插入链表末尾时,才会触发树化判断,即:如果当前链表长度大于等于8,执行树化【treeifyBin(tab, hash)】,执行树化时会判断当前table的长度,如果长度<64,进行resize而不是树化;图中,转为红黑树后,并不是红黑树插入,是已经插入链表后,才进行的红黑树转换

@suozq1
Copy link

suozq1 commented Apr 8, 2022

该图多处细节跟源码出入较大,建议作者重新绘制,向Map中put是,如果是替换,size是不会++的,只有插入新的key,才会执行++

@daizuojian
Copy link

CSDN上这个画的的可以

image

https://blog.csdn.net/citywu123/article/details/122125093

@un1ink
Copy link

un1ink commented Mar 16, 2023

感觉作者改的,跟问题反馈的没啥关系,树化是在插入链表后,只有插入链表末尾时,才会触发树化判断,即:如果当前链表长度大于等于8,执行树化【treeifyBin(tab, hash)】,执行树化时会判断当前table的长度,如果长度<64,进行resize而不是树化;图中,转为红黑树后,并不是红黑树插入,是已经插入链表后,才进行的红黑树转换

补充一下,扩容时此时binCount为7(即p指向第8个节点,p.next == null),再将新节点插入至链表末尾(注,此时该链表已有9个节点),随后触发(binCount >= TREEIFY_THRESHOLD - 1)再执行treeifyBin(tab, hash)。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Content error enhancement New feature or request or suggestion
Projects
None yet
Development

No branches or pull requests

7 participants
@suozq1 @Snailclimb @un1ink @mygao8 @daizuojian @eazydripz and others