mapss.put("name", "2");
mapss.put("name", "11");
System.out.println(mapss.get("name"));
For example, if the output is 11, is there any way to get the value of 2?
Previously, remember to store hashmap is to first calculate the hashcode value of key, and if this position is found to be empty, store it in; If it is not empty, a linked list is formed.
Isn’t that the case? Ask the Great God for an explanation
Your understanding is not comprehensive.
HashMap
Will use firstKey
TheHash value
To check if it happenedHash collision
, that is, whether the corresponding position is empty, this is no problem. The problem is when it happensHash collision
When it does, it compares each one stored in that locationKey
Whether it is equal to the newly deposited, if so, replace it, otherwise, add a value at this position. Obviously, the first two in your codeKey
Is the same, so the back will replace the front.so
HashMap
The time complexity of is notO(1)
If the collision is “intense”, its performance will be reduced. The solution is to increase the capacity, thus reducing the probability of collision, and the performance will naturally become higher. This is what time for space did.