Adding value to existing map key
So I have a map:
Map<String, Integer> totals = new LinkedHashMap<String, Integer>();
I'm going to have about 6 running totals and I figured rather than creating 6 different variables, why not put it into a map. The only problem I have is, how do you add a value to an existing map entry? The map only seems to have the put method so I'm not sure how to go about this?
Any ideas? Am I over-complicating the situation?
Re: Adding value to existing map key
Nevermind. I think I'm over-complicating it because now I have to check to see if the key exists or not... gah. Not worth it.
Re: Adding value to existing map key
Quote:
The map only seems to have the put method
Are you sure? I think it has many methods.
Re: Adding value to existing map key
Well, it obviously has many methods, but it doesn't seem to have another method for adding a value to an existing key. I guess I could use put to add the existing value of that key to the new value but again, I have to check to see if the key exists yet or not and that's just a lot of extra processing.
Re: Adding value to existing map key
Quote:
method for adding a value to an existing key.
The key is associated with one value.
What do you mean by "adding a value" to a key?
You can change the value of the value object or you can replace the value object that is associated with the key.
Yes that will require some processing.
Re: Adding value to existing map key
Well yeah, I understand how the map works. I guess what I want is:
mymap.add("key", value);
And that doesn't seem to exist. And I guess it makes sense that it wouldn't be there by default since maps can have any datatype as the value, so whatever. I went another route this time.
Re: Adding value to existing map key
What would the add() method do?
Re: Adding value to existing map key
Take the new value, add it to the existing value for the provided key.
Re: Adding value to existing map key
Then you'd need a remove() method and a update() method and so on and on
What would be the result of add() when the current value is a String and the value to be added is a JComponent?
Re: Adding value to existing map key
Well yeah, so maybe what I'm looking for is a map that is extended for this purpose.
You'd get a error if you tried to pass in a non-string... not sure what you're getting at?
Re: Adding value to existing map key
Just talking about programming.
Re: Adding value to existing map key
I do this for web stats - counting hits on an URL, visits from an IP address, visits from a country, counting the number of each HTTP response status code, that kind of thing. I use a HashMap for each 'kind' of count, and use an 'increment' method that as has arguments the HashMap and the key. The method retrieves the existing total and increments it, puts it back in, or creates a '1' entry if the key is new.
If you know in advance that you're only ever going to have 6 totals, it seems a waste of time: just create 6 variables.
Re: Adding value to existing map key
Yeah. Not worth it for this instance. If it would be something different, it might be worth exploring.