Results 1 to 4 of 4
  1. #1
    tahni is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Default HashMap functions get vs put

    Hi!

    I recently created a programme where I used a HashMap for storing a large number of (key, value) pairs. I needed to check if an entry was present and if not, add the entry and if so, change the value of the entry.
    In the code below, CoOccurrence is an object storing a key and value pair.

    I first tried with:
    Java Code:
    public void updateHashMap(CoOccurrence co) {
    		CoOccurrence temp = this.get(co.getKey());
    		if( temp != null ){
    			temp.increaseValueByOne();
    	        }
    		else{
    			this.put(co.getKey(),co.getValue());
    		}
    	}
    This way of going about made my programme run for hours, actually up to 12 hours. Then I decided to try the following:
    Java Code:
    public void updateHashMap(CoOccurrence co) {
    		CoOccurrence temp = this.put(co.getKey(), co.getValue());
    		if( temp != null ){
    			temp.increaseValueByOne();
    			this.put(co.getKey(), temp.getValue());
    		}
    	}
    and this only took seconds to execute.
    Anyone with a guess as to why it behaved like this? Is the put-operation faster than the get?

    -tahni.
    Last edited by tahni; 01-14-2009 at 11:20 AM.

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    temp != null?
    at the very beginning, hashmap contains no key-value pairs...

    in second method, you run if statement and always return false

    and in first method, temp.incrementFrequency(), had incrementFrequency method put the updated value to hashmap "this"?
    Last edited by mtyoung; 01-14-2009 at 06:26 AM.

  3. #3
    tahni is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Default

    First of all, my bad, it should be increaseValueByOne() in both versions of the code. I renamed so it would be more intuitive.


    ""temp != null?
    at the very beginning, hashmap contains no key-value pairs...""

    Yes, at first there are no key-value pairs in the hashmap and hence we keep adding key-value pairs until the first time we find one that we have already encountered before, then temp != null and we get to increase the value.


    ""in second method, you run if statement and always return false""

    No, when doing a put operation on a HashMap it will return null if the object is new and the old object if it is already in the HashMap. Look it up under the java api.

    And for your last comment, no, the increaseValueByOne function does not 'replace' the object in the HashMap. This is as I understand it not necessary since we change the object it self and the hashmap is only pointing to that object.

    Thanks for your input.
    -tahni

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    There is no reason, from what you posted, for your program to loop at all. I assume the class that contains the method extends HashMap, since you use "this." You could just use an ArrayList, since you are incrementing the "value" contained in the key. The value object in the HashMap will always be whatever the key's value was when it was first added, which seems a bit odd.

    I suggest looking at whatever is calling this method to see what is causing the looping. Also, does CoOccurrence override equals()? If not, why would you call your method with the same instance more than once? I'm not sure this code is accomplishing what you want it to do.

Similar Threads

  1. Functions in jsp
    By samson in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 03-25-2009, 11:04 PM
  2. Using functions that return values?
    By Megapixelz in forum New To Java
    Replies: 1
    Last Post: 04-30-2008, 05:07 AM
  3. Calculating trigonometric functions
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:56 PM
  4. Calculating hyperbolic functions
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:55 PM
  5. Quick Question (Functions)
    By ibanez270dx in forum New To Java
    Replies: 2
    Last Post: 11-16-2007, 02:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •