Results 1 to 4 of 4
Thread: HashMap functions get vs put
- 01-13-2009, 01:16 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 2
- Rep Power
- 0
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:
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.get(co.getKey()); if( temp != null ){ temp.increaseValueByOne(); } else{ this.put(co.getKey(),co.getValue()); } }
and this only took seconds to execute.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()); } }
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 10:20 AM.
- 01-14-2009, 05:18 AM #2
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
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 05:26 AM.
- 01-14-2009, 10:30 AM #3
Member
- Join Date
- Jan 2009
- Posts
- 2
- Rep Power
- 0
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
- 01-14-2009, 06:55 PM #4
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
-
Functions in jsp
By samson in forum JavaServer Pages (JSP) and JSTLReplies: 3Last Post: 03-25-2009, 10:04 PM -
Using functions that return values?
By Megapixelz in forum New To JavaReplies: 1Last Post: 04-30-2008, 04:07 AM -
Calculating trigonometric functions
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 10:56 PM -
Calculating hyperbolic functions
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 10:55 PM -
Quick Question (Functions)
By ibanez270dx in forum New To JavaReplies: 2Last Post: 11-16-2007, 01:42 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks