Suppose we have a HashMap<String, Integer>
in Java. 假設咱們在Java中有一個HashMap<String, Integer>
。 app
How do I update (increment) the integer-value of the string-key for each existence of the string I find? 如何爲找到的每一個字符串更新(遞增)字符串鍵的整數值? spa
One could remove and reenter the pair, but overhead would be a concern. 能夠刪除並從新進入該對,可是開銷將是一個問題。
Another way would be to just put the new pair and the old one would be replaced. 另外一種方法是隻放置新的一對,而舊的將被替換。 .net
In the latter case, what happens if there is a hashcode collision with a new key I am trying to insert? 在後一種狀況下,若是哈希碼與我要插入的新密鑰發生衝突怎麼辦? The correct behavior for a hashtable would be to assign a different place for it, or make a list out of it in the current bucket. 哈希表的正確行爲是爲其分配一個不一樣的位置,或在當前存儲桶中列出該列表。 code