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源码分析中put的方法的流程图的疑问 #608

Open
zhongshanhao opened this issue Dec 31, 2019 · 4 comments
Open

HashMap源码分析中put的方法的流程图的疑问 #608

zhongshanhao opened this issue Dec 31, 2019 · 4 comments
Labels

Comments

@zhongshanhao
Copy link

@zhongshanhao zhongshanhao commented Dec 31, 2019

图中若key存在,直接覆盖其节点值,这时不应该将size加1吧?按照源码分析应该直接返回被覆盖之前的值吧?
@LiWenGu
Copy link
Contributor

@LiWenGu LiWenGu commented Dec 31, 2019

直接覆盖之后应该就会 return,不会有后续操作。这个图确实有问题。
参考 JDK8 HashMap.java 658 行

@zhongshanhao
Copy link
Author

@zhongshanhao zhongshanhao commented Dec 31, 2019

直接覆盖之后应该就会 return,不会有后续操作。这个图确实有问题。
参考 JDK8 HashMap.java 658 行

确实,源码实现是直接返回被覆盖的值的。

@softwareCQT
Copy link

@softwareCQT softwareCQT commented Mar 29, 2020

如果是putIfAbsent的话才不会被覆盖掉,返回原来的值,主要看那个onlyIfAbsent

@hokage123
Copy link

@hokage123 hokage123 commented Jun 10, 2020

PutIfAbsent Or OnlyIfabsent serve what purpose
If Put() method work for the key if it is not present
Otherwise it overrides the value of key.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.