Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

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

Asked by Drucilla Tutt Apr 19, 2021 1.2K views 1 answer
Share

About this question

Suppose we have a HashMap 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 re-enter the pair, but the 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.

Your answer

1 Answer

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.