current position:Home>Why return oldvalue after the new value overwrites the old value in the put method of HashMap

Why return oldvalue after the new value overwrites the old value in the put method of HashMap

2022-02-03 02:39:05 CSDN Q & A

img




Refer to the answer 1:

Verify the following results , It's true

```javaHashMap<String,String> map = new HashMap<>();        String str = map.put("aaa","aaaa");   str = null        str = map.put("aaa","bbbb");  str = aaaa

```




Refer to the answer 2:



Refer to the answer 3:

This depends on the source code , That's what they wrote ,




Refer to the answer 4:



Refer to the answer 5:

First assign a value to the return variable , Give again key assignment ?




Refer to the answer 6:



Refer to the answer 7:

Because I want to be with you putIfAbsent bring into correspondence with , return key The corresponding old value




Refer to the answer 8:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202030239037995.html

Random recommended