Asked • 05/05/19

How to update a value, given a key in a java hashmap?

Suppose we have a `HashMap<String, Integer>` in Java. How do I update (increment) the integer value of the string key for each existence of the string I find? 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. 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.

1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.