给定哈希图中的键,如何更新值? - How to update a value, given a key in a hashmap?

问题:

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


解决方案:

参考一: https://stackoom.com/question/HRg4/给定哈希图中的键-如何更新值
参考二: https://oldbug.net/q/HRg4/How-to-update-a-value-given-a-key-in-a-hashmap
相关文章
相关标签/搜索