• Post Reply Bookmark Topic Watch Topic
  • New Topic

HashMap advance counter  RSS feed

 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have a HashMap with Integer values and I want to keep advancing those values programatically, can I change the value within the HashMap or do I have to pull out the value, advance it, remove the old key-value pair from the HashMap and put the updated value back in?

for example with an array I could just go like:

obviously, I have Integers and not ints in the HashMap, but is there an easy way to do this with a HashMap?
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tim McGuire:
If I have a HashMap with Integer values and I want to keep advancing those values programatically, can I change the value within the HashMap or do I have to pull out the value, advance it, remove the old key-value pair from the HashMap and put the updated value back in?

for example with an array I could just go like:

obviously, I have Integers and not ints in the HashMap, but is there an easy way to do this with a HashMap?


Mmm, I think you cannot do that without pulling out the value from the HashMap first... You need to use the second approach that you mentioned before... But if you use HashTable, you don't need to remove both key and value... Using HashTable, you just pull the corresponding value, update it and set them using the corresponding key...

Hope this helps...
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, that makes perfect sense.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!