Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Adding value to existing map key

  1. #1
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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?


  2. #2
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,145
    Thanks
    48
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Adding value to existing map key

    The map only seems to have the put method
    Are you sure? I think it has many methods.

  4. #4
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,145
    Thanks
    48
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Adding value to existing map key

    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.

  6. #6
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,145
    Thanks
    48
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Adding value to existing map key

    What would the add() method do?

  8. #8
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Adding value to existing map key

    Take the new value, add it to the existing value for the provided key.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,145
    Thanks
    48
    Thanked 2,158 Times in 2,131 Posts

    Default 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?

  10. #10
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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?

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,145
    Thanks
    48
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Adding value to existing map key

    Just talking about programming.

  12. #12
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default 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.

  13. #13
    Member
    Join Date
    Feb 2011
    Posts
    40
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. Calling Existing DLL functions using java...
    By orihsoyk in forum Java Theory & Questions
    Replies: 0
    Last Post: April 6th, 2011, 07:23 AM
  2. Non-existing loop
    By Neo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 13th, 2011, 06:23 PM
  3. Need help implenting arrays and method call into existing code
    By hoven in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 31st, 2011, 12:36 AM
  4. Add code to an existing project
    By atul.mathur31 in forum Java IDEs
    Replies: 1
    Last Post: January 5th, 2011, 05:50 PM
  5. How to append data to an already existing file?
    By siteregsam in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 3rd, 2010, 02:31 PM